Cod sursa(job #3216601)

Utilizator lalabengBizniz Man lalabeng Data 18 martie 2024 13:37:23
Problema NFA Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <unordered_map>
#include <vector>
#include <deque>
using namespace std;
ifstream f("nfa.in");
ofstream g("nfa.out");
const int N=1e4+5;
int n,x,k;
int nr_fn,fn[1001];
char z;
int m,y,start,query;
unordered_map<int,int>v;
struct pct
{
    int x,y;
};
deque<pct>st;
vector<pct>a[N];

int main()
{
    f>>n>>m>>nr_fn>>start;
    for(int i=1;i<=nr_fn;i++)
        f>>fn[i];
    for(int i=1;i<=m;i++)
    {
        f>>x>>y>>z;
        a[x].push_back({y,z});
    }
    f>>query;
    while(query--)
    {
        string s;
        f>>s;
        st.clear();
        st.push_back({start,0});
        bool ok=false;
        while(!ok&&!st.empty())
        {
            pct nod=st.front();
          //  g<<nod.x<<" "<<nod.y<<'\n';
            st.pop_front();
            if(nod.y==s.size())
            {
                for(int i=1;i<=nr_fn;i++)
                    if(fn[i]==nod.x)
                        ok=true;
                continue;
            }
            for(auto vec: a[nod.x])
            {
                if(vec.y==s[nod.y])
                {
                    st.push_back({vec.x,nod.y+1});
                }
            }
        }
        g<<ok<<'\n';
    }
    return 0;
}