You are not logged in. Please login at www.codechef.com to post your questions!

×

ERROR - Editorial

PROBLEM LINK:

Practice
Contest

Author: Vivek Hamirwasia
Tester: Mahbub
Editorialist: Jingbo Shang

DIFFICULTY:

Cakewalk

PREREQUISITES:

Programming Language.

PROBLEM:

Determine whether the given binary string contains the substring (consecutive) "010" or "101".

EXPLANATION:

You can use a loop and condition clauses to check directly. Also, you can use some built-in functions to solve this problem too.

For example, C++, we can use

int position = strstr(s, "010") - s;

to get the first occurrence of "010" and check whether this position is in range. Or, if string is used in C++, then

int position = s.find("010");

will work for string.

Similarly, Java, Python, etc... a lot of languages have such functions. Post your accepted solution and let's find which one is the shortest! I think it will be interesting :P

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

asked 13 Jan '14, 15:11

shangjingbo's gravatar image

3★shangjingbo ♦♦
161446375
accept rate: 0%

edited 22 Apr '15, 17:36

admin's gravatar image

0★admin ♦♦
19.8k350498541

4

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

(14 Jan '14, 11:14) ajit_a2★

@ rd13123013 When 010 or 101 comes at the last of the string ..then d=='\n' .Therefore ,it doesn't enters into While loop...so the count doesn't increasing .....and there lies the bug.

 So , you can overcome this by using "do() ...while()..loop" :)
link

answered 11 Jul '14, 16:29

neal95's gravatar image

3★neal95
318712
accept rate: 10%

edited 11 Jul '14, 16:32

Using Python's elegance http://www.codechef.com/viewsolution/3250941

for _ in xrange(input()):
    s = raw_input()
    if "010" in s or "101" in s: print "Good"
    else:                        print "Bad"

P.S.: Waiting for a one liner in Perl or GolfScript :P

UPDATE: If that's the case @shangjingbo, then how about a Perl regex check

$s =~ m/"101"/
link

answered 13 Jan '14, 15:23

bugkiller's gravatar image

3★bugkiller
8.7k194898
accept rate: 9%

edited 13 Jan '14, 19:57

1

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

(13 Jan '14, 18:53) shangjingbo ♦♦3★

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

(13 Jan '14, 19:26) bugkiller3★

Wow, great!

(13 Jan '14, 22:40) shangjingbo ♦♦3★

Well, when I'm good with Monads I can return and fully solve this one in Haskell, but, the built-in functions sure are a charm:

isInfixOf "010" "10010"

:D

link

answered 13 Jan '14, 16:29

kuruma's gravatar image

3★kuruma
17.7k72143209
accept rate: 8%

I think it may be the shortest one :)

(13 Jan '14, 18:52) shangjingbo ♦♦3★

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 :D

(13 Jan '14, 21:09) kuruma3★

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.

link

answered 31 Jan '17, 09:28

seraph_wedd18's gravatar image

0★seraph_wedd18
111
accept rate: 0%

@all:

May i know what is wrong with this approach. http://www.codechef.com/viewsolution/3177973

I was checking characters at i, i-1, i-2 in a loop.

Thanks :)

link

answered 14 Jan '14, 16:49

squal's gravatar image

4★squal
1.2k41533
accept rate: 14%

1

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

1
1

your code returns 'Good'...

(14 Jan '14, 17:00) betlista ♦♦3★

feeling bad to have missed this case :( thanks

(14 Jan '14, 17:07) squal4★

don't be sad, I can write a book about stupid bugs I did in contests :-D

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 (http://www.codechef.com/viewsolution/3153331 ) for this problem if you want or this one (http://www.codechef.com/viewsolution/3250870 ), to see how to use StringTokenizer.

(14 Jan '14, 17:46) betlista ♦♦3★

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; }

(15 Jan '14, 02:10) rd131230135★

Try pasting ur code after pressing CTRL+K

(01 Aug '15, 21:35) shubham992★

what is wrong wid this...

include <stdio.h>

include <conio.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")); } getch(); return 0; }

link

answered 15 Jan '14, 02:09

rd13123013's gravatar image

5★rd13123013
0
accept rate: 0%

int main() { int t; cin>>t; while(t--) { std::string str(""); cin>>str; if((str.find("010")==!std::string::npos) || (str.find("101")==!std::string::npos)) { cout<<endl<<"Good"<<endl; } else { cout<<endl<<"Bad"<<endl; } } system("pause"); return 0; }

link

answered 24 May '15, 23:19

bhawin91's gravatar image

1★bhawin91
1
accept rate: 0%

What is wrong with this code? Similar code accepted in java but this is not working.

class Sample7

@t=0 @str

def initialize t=gets.to_i

for i in 0...t
  str=gets
  if str.include?"101"||"010"
    puts "Good"
  else puts "Bad"
  end
end

end end

obj=Sample7.new

link

answered 10 Jun '16, 17:19

s9w311's gravatar image

1★s9w311
1
accept rate: 0%

edited 10 Jun '16, 17:21

WHAT IS WRONG IN MY CODE?

include<iostream>

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; }

link

answered 21 Jul '16, 13:12

geethika1808's gravatar image

0★geethika1808
1
accept rate: 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")
link

answered 06 Oct '16, 22:27

s_pandey01's gravatar image

0★s_pandey01
1
accept rate: 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")
link

answered 06 Oct '16, 22:28

s_pandey01's gravatar image

0★s_pandey01
1
accept rate: 0%

include<iostream>

include<string>

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

answered 30 Jan '17, 22:49

shubham_364's gravatar image

4★shubham_364
1
accept rate: 0%

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!..... :-P

link

answered 06 Apr '17, 16:58

palash7563's gravatar image

1★palash7563
153
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,719
×1,662
×948
×51

question asked: 13 Jan '14, 15:11

question was seen: 5,534 times

last updated: 06 Apr '17, 16:58