Cod sursa(job #2447941)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 15 august 2019 10:07:44
Problema Aho-Corasick Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.45 kb
#include <bits/stdc++.h>

using namespace std;

const int L_MAX = 1000002;
const int N_MAX = 102;
const int BUFFER_SIZE = 4095;

FILE* file = fopen("ahocorasick.in", "r");
ofstream fout ("ahocorasick.out");

char buffer[BUFFER_SIZE];
int pos = -1;

void read_buffer ()
{
    fread(buffer, sizeof(char), BUFFER_SIZE, file);
}

char read ()
{
    pos++;
    if(pos >= BUFFER_SIZE)
    {
        read_buffer();
        pos = 0;
    }
    return buffer[pos];
}

void read_int (int &x)
{
    char c;
    while(iswspace(c = read()));
    x = 0;
    while(isdigit(c))
    {
        x = x * 10 + c - '0';
        c = read();
    }
}

void read_str (char *str)
{
    char c;
    while(iswspace(c = read()));
    string s = "";
    while(c >= 'a' && c <= 'z')
    {
        s += c;
        c = read();
    }
    strcpy(str, s.c_str());
}

struct Trie
{
    vector <int> indexes;
    int ap;
    Trie* sons[26];
    Trie* maxPref;
    Trie* maxW;
    int apSubstr;
    Trie ()
    {
        indexes.clear();
        for(int i = 0; i < 26; i++)
            sons[i] = NULL;
        ap = 0;
        maxPref = NULL;
        maxW = NULL;
        apSubstr = 0;
    }
};

Trie* Root = new Trie();

void trieInsert (Trie* root, char* str, int index)
{
    if(str[0] == '\0')
    {
        root->ap++;
        root->indexes.push_back(index);
    }
    else
    {
        if(root->sons[str[0] - 'a'] == NULL)
            root->sons[str[0] - 'a'] = new Trie();
        trieInsert(root->sons[str[0] - 'a'], str + 1, index);
    }
}

char a[L_MAX];
int n;
char w[N_MAX][L_MAX];

queue <Trie*> q;

vector <Trie*> order;

void bfs ()
{
    Root->maxPref = Root->maxW = Root;
    q.push(Root);
    while(!q.empty())
    {
        Trie* root = q.front();
        order.push_back(root);
        q.pop();
        for(int i = 0; i < 26; i++)
            if(root->sons[i] != NULL)
            {
                root->sons[i]->maxPref = root->maxPref;
                while(root->sons[i]->maxPref != Root && root->sons[i]->maxPref->sons[i] == NULL)
                    root->sons[i]->maxPref = root->sons[i]->maxPref->maxPref;
                if(root->sons[i]->maxPref->sons[i] != NULL && root != Root)
                    root->sons[i]->maxPref = root->sons[i]->maxPref->sons[i];
                root->sons[i]->maxW = root->sons[i]->maxPref;
                if(root->sons[i]->maxW->ap == 0)
                    root->sons[i]->maxW = root->sons[i]->maxW->maxW;
                q.push(root->sons[i]);
            }
    }
}

int answer[L_MAX];

void getFreq ()
{
    Trie* p = Root;
    int lga = strlen(a);
    for(int i = 0; i < lga; i++)
    {
        while(p != Root && p->sons[a[i] - 'a'] == NULL)
            p = p->maxPref;
        if(p->sons[a[i] - 'a'] != NULL)
            p = p->sons[a[i] - 'a'];
        p->apSubstr++;
    }
}

void getAnswer (Trie* node, string s = "")
{
    reverse(order.begin(), order.end());
    for(Trie* node : order)
    {
        node->maxPref->apSubstr += node->apSubstr;
        for(int i : node->indexes)
            answer[i] = node->apSubstr;
    }
}

int main()
{
    read_buffer();
    read_str(a);
    read_int(n);
    for(int i = 1; i <= n; i++)
    {
        read_str(w[i]);
        trieInsert(Root, w[i], i);
    }
    bfs();
    getFreq();
    getAnswer(Root);
    for(int i = 1; i <= n; i++)
        fout << answer[i] << "\n";
    return 0;
}