Skip to content

Instantly share code, notes, and snippets.

@agr-shrn
Last active September 7, 2015 18:34
Show Gist options
  • Save agr-shrn/97d8984855b3774094da to your computer and use it in GitHub Desktop.
Save agr-shrn/97d8984855b3774094da to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream obj;
char ch,str[30],input;
int k=0,initial=9,final[5],num_stats=0,num_inputs=0,num_final,q_prev,q_new,i=0,j=0,mat[5][5];
obj.open("input.txt", ios::in);
obj>>initial;
obj.get(ch);
while((ch = obj.get()) != '\n')
{
if(ch == ' ')
continue;
else
final[i++] = ch - '0';
}
num_final = i;
obj >> num_stats;
obj.get(ch);
i = 0;
while((ch = obj.get()) != EOF)
{
if (ch == '\n') // new line found
{
i++;
j=0;
}
else if(ch == '-')
{
mat[i][j] = -1;
j++;
}
else if(ch == ' ')
{
// ignore spaces
}
else
{
mat[i][j]=ch-'0';
j++;
}
}
num_inputs = j-1;
cin>>str;
i = 0;
q_prev = initial;
q_new = initial;
while(str[i] != '\0')
{
input = str[i] - '0';
q_new = mat[q_prev][input];
if (q_new == -1)
{
// transition when no output edge!
break;
}
q_prev = q_new;
i++;
}
//int flag = 0;
for(k = 0; k < num_final; k++)
{
if(q_new == final[k])
{
cout << "Accepted!";
break;
}
}
if(k == num_final)
cout<<"Not Accepted";
}
/*
input.txt
0
3
2 1
2 -
1 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment