Skip to content

Instantly share code, notes, and snippets.

@yuji314159
Created June 8, 2013 15:05
Show Gist options
  • Save yuji314159/5735444 to your computer and use it in GitHub Desktop.
Save yuji314159/5735444 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
int main(void)
{
int nstate, nalphabet;
int transition[64][10];
int start_state;
int naccept_state, accept_state[64] = {0};
// read definition
scanf("%d %d", &nstate, &nalphabet);
for (int i = 0; i < nstate; ++i) {
for (int j = 0; j < nalphabet; ++j) {
scanf("%d", &transition[i][j]);
}
}
scanf("%d", &start_state);
scanf("%d", &naccept_state);
for (int i = 0; i < naccept_state; ++i) {
int n;
scanf("%d", &n);
accept_state[n] = 1;
}
// skip return
{
char buf[256];
fgets(buf, 255, stdin);
}
// test strings
for (;;) {
char str[256];
int len, state;
// read string
if (fgets(str, 255, stdin) == NULL)
break;
len = strlen(str) - 1;
str[len] = '\0';
// run automaton
state = start_state;
for (int i = 0; i < len; ++i) {
char a = str[i] - '0';
state = transition[state][a];
}
// print result
if (accept_state[state])
printf("accepted (%s)\n", str);
else
printf("rejected (%s)\n", str);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment