What is wrong with my this code, it is working fine for all tests which i could think.
Please can someone tell where it is failing ???
https://www.codechef.com/viewsolution/13954302
what is wrong in my code
#include<stdio.h>
int main()
{
int T,i;
scanf("%d",&T);
for(i=0;i<T;i++)
{
int n,c=0,d=0,u=0,p=0,h=0,s=0,j,e=-1;
scanf("%d",&n);
char A[2][n+1];
scanf("%s",A[0]);
scanf("%s",A[1]);
for(j=0;j<n;j++)
{
if(s==0)
{
if((A[0][j]=='*')&&(A[1][j]=='*'))
{
c++;
h=1;
p=1;
s=1;
u=1;
d=1;
e=c;
}
else if(A[0][j]=='*')
{
u=1;
s=1;
p=1;
}
else if(A[1][j]=='*')
{
d=1;
s=1;
p=1;
}
}
else
{
if((A[0][j-1]=='*')&&(p==1)&&(u==0)&&(e<c))
{
u=1;s=1;e=c;
}
if((A[1][j-1]=='*')&&(p==1)&&(d==0)&&(e<c))
{
d=1;s=1;e=c;
}
if((A[0][j]=='*')&&(A[1][j]=='*'))
{
if(h==0)
{
c++;
h=1;
if((u==1)||(d==1))
{
c++;
u=0;
d=0;
}
}
else
{
if((u==1)||(d==1))
{
c++;
u=0;
d=0;
}
}
}
else if(A[0][j]=='*')
{
if(h==0)
{
if(d==1)
{
h=1;
c++;
}
else if(u==1)
{
c++;
u=0;
d=0;
}
}
else
{
if(u==1)
{
c++;
u=0;
d=0;
}
}
}
else if(A[1][j]=='*')
{
if(h==0)
{
if(u==1)
{
h=1;
c++;
}
else if(d==1)
{
c++;
u=0;
d=0;
}
}
else
{
if(u==1)
{
c++;
u=0;
d=0;
}
}
}
}
}
if(s==1)
printf("%d\n",c);
else
printf("%d\n",0);
}
return 0;
}
The main logic was to find that for the test case(s) with
1010 0101 10|10 _____ 01|01
The answer would be 2 (one horizontal and one vertical). Basically, you need to find every alternating snake occupied houses sub-array. Count the number of alternating snake occupied houses (say, x) and the number of walls in this sub-array would be (x-1)/2. (in this case, (4-1)/2 = 1, plus the horizontal one)
You don’t need to put a vertical wall between 2 alternating snake occupied houses if the horizontal wall already separates them.
Yes, I tested for that exception at the beginning.
@devilhector My code for only one line containing 's was to split on "" with a -1 modifier and subtract 2 from the resulting length. Through numerous copy and pasting, the -2’s became -1’s from an earlier submission. This is very frustrating.
I have a much simpler solution than this official solution.
i have done the question in exactly the same manner still i got wrong answer . I dont have enough karma to upload my solution . Is thera any way that i can get to know which case i am missing
whats wrong in This … ?
for _ in range(int(input())):
n = int(input())
s1 = str(input())
s2 = str(input())
s3 = str()
d = 0
for i in range(n):
if s1[i] == '*' and s2[i] == '*':
d+=1
break
for j in range(n):
if s1[j] == '*' or s2[j] == '*':
s3+='*'
if d == 1 or len(s3) == 0:
print(len(s3))
else:
print(len(s3) - 1
I used a different approach, was getting a wrong answer but couldn’t figure a test case for which this will fail. Consider the number of ‘'s in row 1 as stars1, and the number of stars in row2 as stars2. Also, for columns where both row 1 and row 2 have a '’, count them in a variable common. So the approach is that if(common>0), add a fence(the horizontal fence). And then the number of vertical fences that will be added will be= (stars1 + stars2- common -1) .
I cant figure out why will it fail.Can someone point out a test case. Here is the code:
Can any one help me out where I went wrong
https://www.codechef.com/viewsolution/13933535
I was really working from yesterday night and could not find where it is failing.So please and please help me out
#include<bits/stdc++.h>
#define S_ a[0][i]==’’ && a[1][i]==’.’
#define _S a[0][i]==’.’ && a[1][i]==’’
#define SS a[0][i]==’’ && a[1][i]==’’
#define __ a[0][i]==’.’ && a[1][i]==’.’
#define SNAKE a[0][i+1]==’’||a[1][i+1]==’’
using namespace std;
int main(){
inputSection
//-----------------------------------------------
int firstIndex = 0;
int lastIndex=0;
for(int i=0;i<n;i++)
{
if(a[0][i]==’’ || a[1][i]==’’)
{
firstIndex = i;
break;
}
}
for(int i=n-1;i>=firstIndex;i–)
{
if(a[0][i]==’’ || a[1][i]==’’)
{
lastIndex = i;
break;
}
}
bool hz = false;
int vt =0;
if(firstIndex==lastIndex && a[0][firstIndex]==’’&&a[1][lastIndex]==’’)
{
hz=true;
}
else if(firstIndex==lastIndex)
{
hz = false;
vt=0;
}
for(int i = firstIndex;i<lastIndex;)
{
if(S_)
{
if(!SNAKE)
{i++;continue;}
i=i+1;
if(S_)
vt++;
else if(SS){hz=true;
vt++;}
else if(_S){
hz= true;
}
}
else if(_S){
if(!SNAKE)
{
i++;
continue;
}
i= i+1;
if(_S)
{
vt++;
}
else if(S_)
{
hz=true;
}
else if(SS){
hz= true;
vt++;
}
}
else if( SS){
if(!SNAKE)
{
i++;
continue;
}
i= i+1;
if(_S)
{
vt++;
hz=true;
}
else if(S_)
{
hz=true;
vt++;
}
else if(SS){
hz= true;
vt++;
}
else{
hz=true;
}
}
else // increment i
{i++;}
}
if(hz)
cout<<vt+1<<endl;
else
cout<<vt<<endl;
}
return 0;
}
**Can AnyOne help me in getting error in this code ** Here SOME Macros are used _–>Dot ans S–>Snake I m checking pair wise snake and next snake and then applying the opartion on vertical line and horizontal line i used a boolean for Hz line if it exists then add 1 to vertical lines…
https://www.codechef.com/viewsolution/13933535
This is the link to mmy code.So please help me out where I went wrong
If anyone want’s to check out a correct
[1]
It's quite simple
[1]: https://www.codechef.com/viewsolution/13932694
Anyone who can tell me a test case ? Here is my code. Getting WA for no reason.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
char a[200010];
char b[200010];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf(" %s",a);
scanf(" %s",b);
int cnt=0;
int upper=0;
int lower=0;
for(int i=0;i<n;i++)
{
if(a[i]=='*')
upper++;
if(b[i]=='*')
lower++;
if(upper>=1 && lower>=1)
{
cnt=1;
break;
}
}
upper=lower=0;
for(int i=0;i<n;i++)
{
if(a[i]=='*' && b[i]=='*')
cnt++;
else if(a[i]=='*')
{
upper++;
cnt++;
}
else if(b[i]=='*')
{
lower++;
cnt++;
}
if(upper==1 && lower ==1)
{
cnt--;
lower=upper=0;
}
if(lower>1 || upper>1)
{
if(a[i]=='*' && b[i]=='.')
{
upper=1;
lower=0;
}
else if(b[i]=='*' && a[i]=='.')
{
lower=1;
upper=0;
}
else
upper=lower=0;
}
//printf("at %d count= %d\n",i,cnt);
}
if(cnt)
cnt--;
printf("%d\n",cnt);
}
return 0;
}
doing i-- in the last loop won’t cause the loop to run infinitely? In c++ code given here?
//ANYHELP why this code is not accepted?
#include
#include
#include
#include
#include
using namespace std;
int main(){
int t;
cin>>t;
for(int r=0;r<t;r++){
int q;
cin>>q;
char s[2][q];
char d;
for(int i=0;i<2;i++){
for(int j=0;j<q;j++)
cin>>s[i][j];
}
int c=0;
for(int i=0;i<2;i++){
for(int j=0;j<q;j++)
if(s[i][j]=='*')c++;
}
int count=0;
int s1=0;
int s2=0;
d='a';
for(int k=0;k<q;k++){
if(s[0][k]=='*' || s[1][k]=='*') count++;
if(s[0][k]=='*') s2=1;
if(s[1][k]=='*') s1=1;
if(s[0][k]=='*' && s[1][k]=='*') d='c';
}
int hor=s1+s2;
if(d=='c'){
if(count-1 == 1 && hor==2 && c>2){cout<<2<<endl; }
if(count-1 == 0 && hor==2 && c==2){cout<<1<<endl; }
if(count-1 == 1 && hor==2 && c==2){cout<<1<<endl; }
if(count-1 > 1 && hor==2 ){cout<<count+hor-2<<endl; }
if(count-1 >= 1 && hor!=2 ){cout<<count-1<<endl; }
if(count-1 ==0 && hor!=2 ){cout<<0<<endl; }
if(count-1 ==0 && hor!=2 && c==2){cout<<1<<endl; }
}
else if(d!=c){
if(count>=1)cout<<count-1<<endl;
else cout<<0<<endl;}
}
return 0;
}
- Blockquote
Can anyone please provide a test case where my code fails?
Solution link : AVh0zv - Online C++0x Compiler & Debugging Tool - Ideone.com
Thanks:)