ERROR - Editorial

WHAT IS WRONG IN MY CODE?
#include
#include<string.h>
using namespace std;
int main()
{
int t;
long int l,i,c,d,j,e;
cin>>t;
while(t–)
{
char s[100005];
cin>>s;
l=strlen(s);
for(i=0;i<l-2;i++)
{
e=2;
c=0;
j=i;
while(e–)
{
if(s[j]!=s[j+1])
c++;
else
break;
j++;
}
if(c==2)
{
cout<<“good”<<endl;
break;
}
}
if(c!=2)
cout<<“bad”<<endl;
}
return 0;
}

Guys can anyone tell what is wrong with this python code for the problem, according to me it is correct.....

lst=[]
a=int(input())
i=0
while i<a:
    str1=input()
    lst.append(str1)
    i=i+1

i=0

for j in lst:
    if '010' in j and '101' in j:
        print("Good")
    else:
        print("Bad")

Guys can anyone tell what is wrong with this python code for the problem, according to me it is correct…

lst=[]
a=int(input())
i=0
while i<a:
    str1=input()
    lst.append(str1)
    i=i+1

i=0

for j in lst:
    if '010' in j and '101' in j:
        print("Good")
    else:
        print("Bad")

#include
#include
using namespace std;
int main(){
int a;
cin>>a;
while(a–){
string s;int c=0;
cin>>s;
for(int i=0;i<(s.length()-1)&&c<2;i++){
if(s[i]!=s[i+1])c++;
else c=0;
}
if(c==2)cout<<“Good”<<endl;
else cout<<“Bad”<<endl;
}
return 0;
}

link text

@s_pandey01
It is in the checking stage. if ‘010’ in j and ‘101’ in j:
it should use ‘or’ instead of ‘and’. If ‘and’ is used, then ‘Good’ will only print out when both ‘010’ and ‘101’ are in the line.

1 Like

import java.util.Scanner;
class untitled
{
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
int test=kb.nextInt();
while(test–>0)
{
String s1=kb.next();
String s2=“010”;
String s3=“101”;
if(s1.contains(s2)||s1.contains(s3))
System.out.println(“Good”);
else
System.out.println(“Bad”);
}
}
}
benefit of java!..
:stuck_out_tongue:

I think it may be the shortest one :slight_smile:

sound great, but you have benn beaten by @kuruma, have another try :smiley:

1 Like

@shangjingbo >> I was not trying to enter any competition for shortest code. I was just demonstrating Python’s elegance. :smiley:

The problem with this check is that it doesn’t receive input and produce output, instead, it can be used in an interactive fashion, but, I’m loving Haskell so far :smiley:

Wow, great!

I foolishly tried to implement “Z- algo”… :P. Someone kick me on my ass…

4 Likes

You missed corner case when length < 3, for example for

1
1

your code returns ‘Good’…

2 Likes

feeling bad to have missed this case :frowning: thanks

don’t be sad, I can write a book about stupid bugs I did in contests :smiley:

My advice here is, do not use some side effects (like if I scanned whole string), use additional boolean flag that is set to false at the beginning and when you really find what you are looking for, set it to true.

Also you do not need to use such difficult input reading, see my code (CodeChef: Practical coding for everyone ) for this problem if you want or this one (CodeChef: Practical coding for everyone ), to see how to use StringTokenizer.

what is wrong with this…
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
char a , b , c ,d ,count ;
scanf("%d" , &t);
while(t–)
{
count = 0;
a = getchar();
b = getchar();
c = getchar();
while( (d = getchar()) != ‘\n’)
( (a==‘0’ && b==‘1’ && c==‘0’) || (a == ‘1’ && b==‘0’ && c==‘1’)) ? ( (count++) , (a = b) , ( b = c) , (c = d )): ((a = b) , ( b = c), (c = d ));
(count > 0)?(printf(“Good\n”)):(printf(“Bad\n”));
}
return 0;
}

Try pasting ur code after pressing CTRL+K

if ‘010’ in j and ‘101’ in j:

use ‘or’ instead of ‘and’, its mentioned in the description of the question.

thanks man

C++ Stl Advantage not shorted as python but yeah little bit short code
#include
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
    string s1;
    cin>>s1;
    int val1=s1.find("010");
    int val2=s1.find("101");
   if(val1>=0||val2>=0){
        cout<<"Good"<<endl;
        }else{
        cout<<"Bad"<<endl;    }
    }

return 0;
}