UVALive 4670 (LA 4670) Dominating Patterns AC自动机

题目大意:

白书例题

对于给出的N个(1 <= N <= 150) pattern (每个长度不超过70) 对于给出的language text string(长度不超过10^6) 输出N个pattern中出现次数最多的

如果有多个结果, 按照输入顺序输出


大致思路:

明摆着个AC自动机水题...

首先对于所有pattern建立AC自动机然后扫描一遍language text string即可, 记录每个字符串出现的次数

细节见代码吧


代码如下:

Result  :  Accepted     Memory  :  ? KB     Time  :  112 ms

/*
 * Author: Gatevin
 * Created Time:  2015/2/11 16:12:01
 * File Name: Mononobe_Mitsuki.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int n;
char in[151][80];
char s[1000010];

struct Trie
{
    int next[151*71][26], fail[151*71], end[151*71];
    int L, root;
    int times[151];
    int newnode()
    {
        for(int i = 0; i < 26; i++)
            next[L][i] = -1;
        end[L++] = 0;
        return L - 1;
    }
    void init()
    {
        L = 0;
        root = newnode();
        memset(times, 0, sizeof(times));
        return;
    }
    void insert(char *s, int id)
    {
        int now = root;
        for(; *s; s++)
        {
            if(next[now][*s - 'a'] == -1)
                next[now][*s - 'a'] = newnode();
            now = next[now][*s - 'a'];
        }
        end[now] = id;
        return;
    }
    void build()
    {
        fail[root] = root;
        queue <int> Q;
        Q.push(root);
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0; i < 26; i++)
                if(next[now][i] == -1)
                    next[now][i] = now == root ? root : next[fail[now]][i];
                else
                {
                    fail[next[now][i]] = now == root ? root : next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
        return;
    }
    void query(char *s)//查询字符串S中各个子串的出现次数
    {
        int now = root;
        for(; *s; s++)
        {
            now = next[now][*s - 'a'];
            int tmp = now;
            while(tmp != root)//沿着fail指针向上回溯计算出现的pattern的次数
            {
                if(end[tmp])
                    times[end[tmp]]++;
                tmp = fail[tmp];
            }
        }
        int maxtimes = 0;
        for(int i = 1; i <= n; i++)
            if(times[i] > maxtimes)
                maxtimes = times[i];
        printf("%d\n", maxtimes);
        for(int i = 1; i <= n; i++)
            if(times[i] == maxtimes)
                printf("%s\n", in[i]);
        return;
    }
};

Trie AC;

int main()
{
    while(scanf("%d", &n), n)
    {
        AC.init();
        for(int id = 1; id <= n; id++)
        {
            scanf("%s", in[id]);
            AC.insert(in[id], id);
        }
        AC.build();
        scanf("%s", s);
        AC.query(s);
    }
    return 0;
}


你可能感兴趣的:(uvalive,AC自动机,patterns,la,4670,4670,Dominating)