Pagini recente » Cod sursa (job #1730740) | Cod sursa (job #3037934) | Istoria paginii runda/3534534535 | Istoria paginii registru-diplome | Cod sursa (job #2636620)
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
ifstream fin("nfa.in");
ofstream fout("nfa.out");
const int DIM = 305;
int n,m,k,s,F[DIM],x,y,q;
bool ok=0;
char c;
vector < pair<int, char> > G[DIM];
string a;
void Read()
{
fin>>n>>m>>k;
fin>>s;
for(int i=1;i<=k;i++)
fin>>F[i];
while(m--)
{
fin>>x>>y>>c;
G[x].push_back(make_pair(y,c));
}
}
void Solve(int node, string a, int pos, int lenght)
{
if(pos==lenght)
{
for(int i=1;i<=k;i++)
{
if(node==F[i])
{
ok=1;
break;
}
}
return;
}
else
{
vector < pair < int, char > > ::iterator it;
for(it=G[node].begin();it!=G[node].end();it++)
{
int next=(*it).first;
char p=(*it).second;
if(p==a[pos] && pos<lenght)
Solve(next,a,pos+1,lenght);
}
}
}
void Query()
{
fin>>q;
while(q--)
{
fin>>a;
ok=0;
Solve(s,a,0,a.size());
fout<<ok<<'\n';
}
}
int main()
{
Read();
Query();
}