@sauravsinha137
https://ideone.com/W9JMuW answer is 3!
whats wrong with my solution-
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while (t--)
{
int n,i;
cin>>n;
char a[2][n];
for (i=0;i<n;i++)
{
cin>>a[0][i];
}
for (i=0;i<n;i++)
{
cin>>a[1][i];
}
string s="";
for (i=0;i<n;i++)
{
if (a[0][i]=='*'&&a[1][i]=='*')
{
s+="h";
}
else if (a[0][i]=='*'&&a[1][i]=='.')
{
s+="u";
}
else if (a[0][i]=='.'&&a[1][i]=='*')
{
s+="d";
}
else if (a[0][i]=='.'&&a[1][i]=='.')
{
s+="n";
}
}
int fences=0;
int x=n-1;
for (i=0;i<(n);i++)
{
if (s[i]=='h'||s[i]=='u'||s[i]=='d')
{
x=i;
break;
}
}
for (i=x+1;i<n;i++)
{
if (s[i]=='h'||s[i]=='u'||s[i]=='d')
{
fences++;
}
}
int hexist=0;
for (i=0;i<n;i++)
{
if (s[i]=='h')
{
hexist=1;
break;
}
}
if (hexist==1)
fences++;
cout<<fences<<endl;
}
return 0;
}
Somebody please correct me where i went wrong?
Here’s my logic:
case 1: if the snakes are in row 1 or 2 or both but not vertically adjacent then i gave solution as total no. of snakes -1.
case 2:if the snakes are in row 1 or 2 or both and least 2 snakes are vertically adjacent then i gave solution as 1 + total no. of snakes -1 - no. of vertically adjacent pairs.
case 3 : if there is 0 or 1 snake in total then i gave solution as 0.
https://www.codechef.com/viewsolution/13947900
Your code gives a compilation error because you have used scanf function for taking test case input without including stdio.h .
Correct me if I’m wrong.
There is an error in your logic for case 1.
Now consider a test case
5
.*.*.
*.*.*
Answer according to your logic will be 4 but correct answer is 3
Can anyone pls explain why is “i” decremented in the Case 1(inside the if condition of for loop)??
@sai_rathan my program i also giving output as 3. As the 1st and 2nd column have vertical adjacent snakes so this is my case 2.
please Suggest in which test case following code fails
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
string arr[2];
cin>>arr[0]>>arr[1];
ll ver=0,hori=0;
ll count=0;
for(ll i=0;i<n;i++)
{
if(arr[0][i]==arr[1][i] && arr[0][i]=='*')
hori=1;
if(arr[0][i]=='*' || arr[1][i]=='*')
count++;
}
ll val=1,up=0,low=0;
for (int i = 0; i < n; ++i)
{
if(arr[0][i]=='*')
up++;
if (arr[1][i]=='*')
low++;
if(up>1)
{
up=1;low=0;val++;
}
if(low>1)
{
low=1;up=0;val++;
}
}
ll k=min(count-1+hori,val);
if(k>0)
cout<<k<<"\n";
else
cout << "0\n";
}
}
using namespace std;
int main()
{
int testcases ;
cin >> testcases ;
for (int t = 0 ; t < testcases ; t++){
int n ;
cin >> n ;
char a[2][n];
for (int i = 0 ; i < 2 ; i++){
for (int j = 0 ; j < n ; j++){
cin >> a[i][j];
}
}
int up_snake = 0 ; // if there is snake up
int down_snake = 0 ; // if there is snake down
int fence = 0 ; // answer
int start_up = 0 ; // first snake up
int start_down = 0 ; // first snake down
for (int i = 0 ; i < n ; i++){
if (a[0][i] == '*'){
up_snake = 1 ;
start_up = i ;
break ;
}
}
for (int i = 0 ; i < n ; i++){
if (a[1][i] == '*'){
down_snake = 1 ;
start_down = i ;
break ;
}
}
if (up_snake == 1 && down_snake == 1){
fence++ ; // for between fence
}
// cout << "basic fence " << fence << endl ;
int start_index = start_up ; // starting index
int count_up = 1 ;
int count_down = 0 ;
if (start_up > start_down){
count_down = 1 ;
count_up = 0 ;
start_index = start_down ;
}
if (start_up == start_down){
count_up = 1 ;
count_down = 1 ;
}
if (start_index == n-1){
cout << 0 << endl ;
}
else {
for (int i = start_index+1 ; i < n ; i++){
if (a[0][i] == '*' && a[1][i] == '*'){
fence ++ ;
count_down = 1 ;
count_up = 1 ;
}
else if (a[0][i] == '*' && count_up == 1){
fence ++ ;
count_down = 0 ;
}
else if (a[0][i] == '*' && count_up == 0){
count_up ++ ;
}
else if (a[1][i] == '*' && count_down == 1){
fence ++ ;
// cout << " fence down at " <<i << endl ;
count_up = 0 ;
}
else if (a[1][i] == '*' && count_down == 0){
count_down ++ ;
}
}
cout << fence << endl ;
}
}
return 0;
}
can anyone tell me in what am i doing wrong ???
#include<stdio.h>
#include<string.h>
int main()
{
int t,n,i,first,second,a,b,count;
char arr[2][100004];
scanf("%d",&t);
while(t–)
{
first=0;
second=0;
count=0;
a=0;b=0;
scanf("%d",&n);
scanf("%s",&arr[0]);
scanf("%s",&arr[1]);
for(i=0;i<n;i++)
{
if(arr[0][i]==’’)
first++;
if(arr[1][i]==’’)
second++;
}
if(first+second<2)
{
printf(“0\n”);
continue;
}
if(first==1&&second==1)
{
printf(“1\n”);
continue;
}
if(first==0)
{
printf("%d\n",second-1);
continue;
}
if(second==0)
{
printf("%d\n",first-1);
continue;
}
for(i=0;i<n;i++)
{
if(arr[0][i]==’’)
a++;
if(arr[1][i]==’’)
b++;
if(a==2)
{
count++;
a–;
if(b>0)
b–;
}
if(b==2)
{
count++;
b–;
if(a>0)
a–;
}
}
printf("%d\n",count+1);
}
}
this is my code
please tell where it is failing
i hope you guys can understand this
i cant upload image due to lack of karma and on pasting the code it appears like this
but there is no compilation error just tell where the logic went wrong
thanks
Share your submission link
Fixed the format.
Users are requested to provide a submission link instead of pasting the entire code.
Your code doesn’t work for the test case below:
Input:
.**
**.
Your code’s output: 2
Expected Output : 3
I tried running your code in both Codechef ide and codeblocks both raised a compilation error. Ok I will try to find an error test case for your code.
Your Code is not working for this case:
Input:
3
.**
**.
Your code’s Output: 2
Expected Output: 3