Pagini recente » Cod sursa (job #2161343) | Cod sursa (job #2153200) | Cod sursa (job #969189) | Rating Grigorascu Andrei Antonio (antonio.gr) | Cod sursa (job #2627864)
#include <bits/stdc++.h>
#define input "nfa.in"
#define output "nfa.out"
#define NMAX 305
using namespace std;
ifstream in(input);
ofstream out(output);
struct muchie
{
int y, c;
};
vector < muchie > muchii[NMAX];
string mesaj;
int N, M, K, Q, BG, ND[NMAX];
bool ans = false, uz[NMAX][NMAX];
void DFS(int nod, int n, string text)
{
if(ans) return;
for(unsigned i = 0; i < muchii[nod].size(); i++)
{
int new_nod = muchii[nod][i].y, val = muchii[nod][i].c;
if(!uz[new_nod][n])
{
uz[new_nod][n] = 1;
if(text.size() < mesaj.size())
{
if(val == (int)mesaj[n])
{
text.push_back(val);
if(text.size() == mesaj.size() && ND[new_nod] == 1)
{
if(text == mesaj) ans = true;
}
else
{
DFS(new_nod, n + 1, text);
}
text.pop_back();
}
}
}
}
}
void Solve()
{
ans = false;
string t;
memset(uz, 0, sizeof uz);
DFS(BG, 0, t);
out << ans << "\n";
}
void Read_Data()
{
in >> N >> M >> K;
in >> BG;
for(int i = 1; i <= K; i++)
{
int x; in >> x;
ND[x] = 1;
}
for(int i = 1; i <= M; i++)
{
int x, y; char c; in >> x >> y >> c;
muchii[x].push_back({y, c});
}
in >> Q;
while(Q--)
{
in >> mesaj;
Solve();
}
}
int main()
{
Read_Data();
out.close();
return 0;
}