您的位置:首页 > 其它

HDU 5384 Danganronpa【AC自动机】

2015-08-13 21:08 232 查看
AC自动机模板题

和HDU 2222不同的是,HDU 2222每次匹配之后就要把end清空,他求的是,模式串中有多少个出现在文本串中。

[code]//      whn6325689
//      Mr.Phoebe
//      http://blog.csdn.net/u013007900 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define speed std::ios::sync_with_stdio(false);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))

#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))

#define MID(x,y) (x+((y-x)>>1))

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int maxw = 100000 + 10;
const int sigma_size = 27;

string str[100010];

struct Trie
{
    int next[maxw][sigma_size],fail[maxw],end[maxw];
    int root,L;
    int newnode()
    {
        for(int i=0; i<sigma_size; i++)
            next[L][i]=-1;
        end[L++]=-1;
        return L-1;
    }
    void init()
    {
        L=0;
        root=newnode();
    }
    void insert(const char *s,int id)
    {
        int now=root,len=strlen(s);
        for(int i=0; i<len; i++)
        {
            if(next[now][s[i]-'a']==-1)
                next[now][s[i]-'a']=newnode();
            now=next[now][s[i]-'a'];
        }
        if(end[now]!=-1)
            end[now]++;
        else
            end[now]=1;
    }
    void build()
    {
        queue<int>Q;
        fail[root]=root;
        for(int i=0; i<sigma_size; i++)
            if(next[root][i]==-1)
                next[root][i]=root;
            else
            {
                fail[next[root][i]]=root;
                Q.push(next[root][i]);
            }
        while(!Q.empty())
        {
            int now=Q.front();
            Q.pop();
            for(int i=0; i<sigma_size; i++)
                if(next[now][i]==-1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(const string &buf)
    {
        int num=0;
        int now=root,len=buf.length();
        for(int i=0; i<len; i++)
        {
            now=next[now][buf[i]-'a'];
            int tmp=now;
            while(tmp!=root)
            {
                if(end[tmp]!=-1)
                    num+=end[tmp];
                tmp=fail[tmp];
            }
        }
        return num;
    }
};

char st[10010];
Trie ac;

int main()
{
//    freopen("data.txt","r",stdin);
    int n,m;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        ac.init();
        for(int i=1; i<=n; i++)
            cin>>str[i];
        for(int i=1; i<=m; i++)
        {
            scanf("%s",st);
            ac.insert(st,i);
        }
        ac.build();
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            ans=ac.query(str[i]);
            printf("%d\n",ans);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: