Cod sursa(job #2593578)

Utilizator coronavirusCovid19 coronavirus Data 4 aprilie 2020 11:35:52
Problema NFA Scor 40
Compilator cpp-64 Status done
Runda corona_day6 Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("nfa.in");
ofstream fout("nfa.out");

int n, m, k, start, l;
bool ok;
bool stop[301];
vector<pair<int, char>>G[301];
char str[151];

void DFS(int nod, int idx)
{
    if(ok) return;
    if(idx == l + 1)
    {
        if(stop[nod])
        {
            ok = 1;
            return;
        }
    }

    for(auto it: G[nod])
    {
        if(it.second == str[idx])
        {
            DFS(it.first, idx + 1);
        }
    }
}

int main()
{
    fin >> n  >> m >> k;
    fin >> start;

    for(int i = 1; i <= k; ++i)
    {
        int x;
        fin >> x;
        stop[x] = 1;
    }

    for(int i = 1; i <= m; ++i)
    {
        int x, y;
        char ch;
        fin >> x >> y >> ch;
        G[x].push_back(make_pair(y, ch));
    }

    int Q;
    fin >> Q;

    while(Q--)
    {
        fin >> (str + 1);
        l = strlen(str + 1);
        ok = 0;
        DFS(start, 1);

        fout << ok << "\n";
    }

    return 0;
}