Skip to content

Instantly share code, notes, and snippets.

@GnsP
Created May 21, 2015 13:28
Show Gist options
  • Select an option

  • Save GnsP/5bf9f6c95cb622df30b2 to your computer and use it in GitHub Desktop.

Select an option

Save GnsP/5bf9f6c95cb622df30b2 to your computer and use it in GitHub Desktop.
TICKETS (Codechef parctice)
#include <cstdio>
using namespace std;
bool dfa(){ // Check what a DFA (Deterministic Finite Automaton) is and how it works.
int state = 0; // DFAs are a bit complex for beginners, because it's an abstract mathematical
char a = getchar(); // concept. But on the other hand the logic used for this specific problem
char b = getchar(); // is very basic. Hence just the basic idea of the DFA concept should suffice.
if(a==b) state = 2;
char ch;
while((ch=getchar()) != '\n'){
if(state == 0){
if(ch==a) state = 1;
else state = 2;
}
else if(state == 1){
if(ch==b) state = 0;
else state = 2;
}
}
return !(state == 2);
}
int main(){
int T;
bool res;
scanf("%d\n",&T);
while(T--){
res = dfa();
if(res) printf("YES\n");
else printf("NO\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment