Pagini recente » Cod sursa (job #1848522) | Cod sursa (job #2318276) | Cod sursa (job #2843996) | Cod sursa (job #2770325) | Cod sursa (job #3230782)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("nfa.in");
ofstream fout ("nfa.out");
map<pair<int, char>, vector<int>> mat;
unordered_set <int> st_final;
string cuv;
bool verifica (unordered_set<int> stare, int poz)
{
if (poz == cuv.size())
{
for (auto x : stare)
if (st_final.find(x) != st_final.end())
return 1;
return 0;
}
unordered_set<int> stari_noi;
for (auto x : stare)
if (mat.find({x, cuv[poz]}) != mat.end())
for (auto y : mat[{x, cuv[poz]}])
stari_noi.insert(y);
if (stari_noi.empty())
return 0;
return verifica(stari_noi, poz + 1);
}
int main()
{
int n, m, x, y, st_init, nrF, nrCuv;
char l;
vector<int>stari;
fin >> n >> m >> nrF;
for (int i = 1; i <= n; i++)
{
stari.push_back(i);
}
fin >> st_init;
for (int i = 1; i <= nrF; i++)
{
fin >> x;
st_final.insert(x);
}
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> l;
if (mat.find({x,l}) == mat.end())
mat[{x, l}] = {y};
else mat[{x,l}].push_back(y);
}
fin >> nrCuv;
for (int i = 1; i <= nrCuv; i++)
{
fin >> cuv;
if (verifica({st_init}, 0))
fout << 1;
else fout << 0;
fout << '\n';
}
return 0;
}