CodeChef Problem: Feedback


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Lots of geeky customers visit our chef’s restaurant everyday. So, when asked to fill the feedback form, these customers represent the feedback using a binary string (i.e a string that contains only characters ‘0’ and ‘1’.
Now since chef is not that great in deciphering binary strings, he has decided the following criteria to classify the feedback as Good or Bad :

If the string contains the substring “010” or “101”, then the feedback is Good, else it is Bad. Note that, to be Good it is not necessary to have both of them as substring.

So given some binary strings, you need to output whether according to the chef, the strings are Good or Bad.
Input

The first line contains an integer T denoting the number of feedbacks. Each of the next T lines contains a string composed of only ‘0’ and ‘1’.
Output

For every test case, print in a single line Good or Bad as per the Chef’s method of classification.
Constraints

1 ≤ T ≤ 100
1 ≤ |S| ≤ 105

Sum of length of all strings in one test file will not exceed 6*106.

Input:
2
11111110
10101010101010

Output:
Bad
Good

Explanation

Example case 1.

The string doesn’t contain 010 or 101 as substrings.
Example case 2.

The string contains both 010 and 101 as substrings.

My Solution


#include <stdio.h>

int checkChars(char tri[]){
  if(tri[0]=='1'&&tri[1]=='0'&&tri[2]=='1')
    return 1;
  else if(tri[0]=='0'&&tri[1]=='1'&&tri[2]=='0')
    return 1;
  else 
    return 0;
}

int main(){
  int t,k;
  char tri[3];
  int i;
  char c;
  int exit;
  int print;

  scanf("%d",&t);
  c = getchar();

  for(k=0;k<t;k++){
    exit = 0;
    print = 1;
    if(k>0)
      printf("\n");
    for(i=0;i<3;i++){
      c = getchar();
      if(c!=10&&c!=EOF)
        tri[i] = c;
      else{
        printf("Bad");
        exit = 1;
        break;
      }      
    }  

    if(exit)
      continue;

    if(checkChars(tri)){
      printf("Good");
      while(1){
        c = getchar();
        if(c==10||c==EOF)
          break;
      }
      continue;
    }

    while(1){
      c = getchar();  
      if(c==10||c==EOF)
        break;
      tri[0] = tri[1];
      tri[1] = tri[2];
      tri[2] = c;
      if(checkChars(tri)){
        printf("Good");
        while(1){
          c = getchar();
          if(c==10||c==EOF)
            break;
        }
        print = 0;
        break;
      }    
    }    
    
    if(print)
      printf("Bad");

  }

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *