Cod sursa(job #2589029)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 25 martie 2020 18:03:05
Problema NFA Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <bits/stdc++.h>
//#include "Automat.cpp"
#include <unordered_set>
using namespace std;
ifstream fin("nfa.in");
ofstream fout("nfa.out");

class Automat{
private:
    int n, m;
    int nod_init;
    bool is_final[310];
    bool am_fost[310][160];
    vector<int> muchie[310][30];
    int n_final_nodes;  
public:
    friend ifstream& operator >> (ifstream& , Automat &);
    bool checkWord(string &, int, int);
    inline void setMem(int);
    inline int init()
    {
        return nod_init;
    }
};

inline void Automat :: setMem(int siz)
{
    for (int i = 0; i<= n; i++)
        for (int j = 0; j <= siz + 1; j++)
            am_fost[i][j] = 0;
}

ifstream& operator >> (ifstream& fin, Automat &obj)
{
    int aux, nods, nodf;
    char c;
    fin >> obj.n >> obj.m >> obj.n_final_nodes;
    fin >> obj.nod_init;
    for (int i = 0 ; i <= obj.n; i++)
    {
        obj.is_final[i] = false;
    }
    for (int i = 1; i<= obj.n_final_nodes; i++)
    {
        fin >> aux;
        obj.is_final[aux] = true;
    }
    for (int i = 1; i<= obj.m; i++)
    {
        fin >> nods >> nodf >> c;
        obj.muchie[nods][c - 'a'].push_back(nodf);
    }
    return fin;
}

bool Automat::checkWord(string &s, int nod, int poz)
{

    if (poz == s.size()) 
    {
        return is_final[nod];
    }
    if (!am_fost[nod][poz])
    {
        am_fost[nod][poz] = true;
        for (auto next : muchie[nod][s[poz] - 'a'])
            if (checkWord(s, next, poz + 1)) return 1;
    }
}


int main()
{

    Automat aut;
    fin >> aut;
    string s;
    int n;
    fin >> n;
    for (int i = 1 ; i <= n; i++)
    {
        fin >> s;
        aut.setMem(s.size());
        fout << aut.checkWord(s, aut.init(), 0)<<"\n";

    }

    
    return 0;
}