Cod sursa(job #2831151)

Utilizator robert.barbu27robert barbu robert.barbu27 Data 10 ianuarie 2022 21:25:55
Problema Aho-Corasick Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.33 kb
#include <bits/stdc++.h>
#define NMAX 300000
#define ll long long
#define pb push_back
using namespace std;
class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
ifstream f("ahocorasick.in");
OutParser g("ahocorasick.out");
string a;
char cuv[500005];
struct Trie
{
    int cnt;
    Trie *fiu[26],*backedge;
    vector<Trie*> v;
    Trie()
    {
        cnt=0;
        for(int i=0; i<26; i++) fiu[i]=NULL;
        backedge=NULL;
        v.clear();
    }

};
stack<Trie*> stk;
Trie *ans[500005];
Trie *root=new Trie;
Trie  *update(Trie *nod, char *s)
{
    if(*s<'a'||*s>'z')
    {
        //nod->cnt++;
        return nod;
    }
    if(nod->fiu[*s-'a']==NULL) nod->fiu[*s-'a']=new Trie;
    return update(nod->fiu[*s-'a'],s+1);
}
void bfs()
{
    queue< Trie* > q;
    q.push(root);
    while(!q.empty())
    {
        Trie *nod= q.front();
        q.pop();
        stk.push(nod);
        for(int i=0; i<26; i++)
        {
            if(nod->fiu[i]!=NULL)
            {
                Trie *bk= nod->backedge;
                while(bk!=NULL&&bk->fiu[i]==NULL)
                {
                    bk=bk->backedge;
                }
                if(bk==NULL) bk=root;
                else bk=bk->fiu[i];
                nod->fiu[i]->backedge=bk;
                //bk->v.push_back(nod->fiu[i]);
                q.push(nod->fiu[i]);
            }
        }
    }
}
void matchwords(string s)
{
    Trie* node = root;
    for(char c: s)
    {
        while(node!=NULL&&node->fiu[c-'a']==NULL)
        {
            node=node->backedge;
        }
        if(node==NULL) node=root;
        else node=node->fiu[c-'a'];
        node->cnt++;
    }
}
void solve()
{
    while(!stk.empty())
    {
        Trie *node=stk.top();
        stk.pop();
        if(node->backedge!=NULL)
        {
            node->backedge->cnt+=node->cnt;
        }
    }
}
int main()
{
    f>>a;
    int n;
    f>>n;
    for(int i=1;i<=n;i++)
    {
        f>>cuv;
        ans[i]=update(root,cuv);

    }
    bfs();
    matchwords(a);
    solve();
    for(int i=1;i<=n;i++)
    {
       g<<ans[i]->cnt<<'\n';
    }



}