PROBLEM LINK:Author: Vivek Hamirwasia 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
to get the first occurrence of "010" and check whether this position is in range. Or, if string is used in C++, then
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.
This question is marked "community wiki".
asked 13 Jan '14, 15:11 ![]()
|
@ 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.
answered 11 Jul '14, 16:29 ![]()
|
Using Python's elegance
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
answered 13 Jan '14, 15:23 ![]()
@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)
Wow, great!
(13 Jan '14, 22:40)
|
@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. answered 31 Jan '17, 09:28 ![]()
|
@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 :) answered 14 Jan '14, 16:49 ![]()
1
You missed corner case when length < 3, for example for
your code returns 'Good'...
(14 Jan '14, 17:00)
feeling bad to have missed this case :( thanks
(14 Jan '14, 17:07)
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 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)
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)
Try pasting ur code after pressing CTRL+K
(01 Aug '15, 21:35)
|
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; } answered 15 Jan '14, 02:09 ![]()
|
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; } answered 24 May '15, 23:19 ![]()
|
What is wrong with this code? Similar code accepted in java but this is not working. class Sample7 def initialize t=gets.to_i
end end obj=Sample7.new answered 10 Jun '16, 17:19 ![]()
|
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; answered 21 Jul '16, 13:12 ![]()
|
answered 06 Oct '16, 22:27 ![]()
|
answered 06 Oct '16, 22:28 ![]()
|
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; } answered 30 Jan '17, 22:49
|
benefit of java!..... :-P answered 06 Apr '17, 16:58 ![]()
|
I foolishly tried to implement "Z- algo".. :P. Someone kick me on my ass...