hdu 6096 ac自动机 和 处理字符串分段输入

Bob has a dictionary with N words in it. 
Now there is a list of words in which the middle part of the word has continuous letters disappeared. The middle part does not include the first and last character. 
We only know the prefix and suffix of each word, and the number of characters missing is uncertain, it could be 0. But the prefix and suffix of each word can not overlap. 
For each word in the list, Bob wants to determine which word is in the dictionary by prefix and suffix. 
There are probably many answers. You just have to figure out how many words may be the answer.
Input
The first line of the input gives the number of test cases T; T test cases follow. 
Each test case contains two integer N and Q, The number of words in the dictionary, and the number of words in the list. 
Next N line, each line has a string Wi, represents the ith word in the dictionary ( 0<|Wi|100000 0<|Wi|≤100000
Next Q line, each line has two string Pi , Si, represents the prefix and suffix of the ith word in the list ( 0<|Pi|,|Si|100000,0<|Pi|+|Si|100000 0<|Pi|,|Si|≤100000,0<|Pi|+|Si|≤100000
All of the above characters are lowercase letters. 
The dictionary does not contain the same words. 

Limits 
T5 T≤5 
0<N,Q100000 0<N,Q≤100000 
Si+Pi500000 ∑Si+Pi≤500000 
Wi500000 ∑Wi≤500000 
Output
For each test case, output Q lines, an integer per line, represents the answer to each word in the list. 
Sample Input
1
4 4
aba
cde
acdefa
cdef
a a
cd ef
ac a
ce f
Sample Output
2
1
1

0

ac自动机 最新板子解法

#include <bits/stdc++.h>
using namespace std;
#define maxm 505000*3
const int M = 505000*3;
char str[maxm];
int ans[M];
const int SIGMA_SIZE=27;

struct AC
{
    int ch[maxm][27];
    int val[maxm];
    int height[maxm];
    int fail[maxm],last[maxm];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return x-'a';}
    int insert(char *s)
    {
        int u=0;
        int n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                height[sz]=height[u]+1;
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]++;
        return u;
    }
    void getfail()
    {
        queue<int> q;
        fail[0]=0;
        int u=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){q.push(u);fail[u]=0;last[u]=0;}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            for(int i=0;i<SIGMA_SIZE;i++)
            {
                u=ch[r][i];
                if(!u){ch[r][i]=ch[fail[r]][i];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][i])v=fail[v];
                fail[u]=ch[v][i];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
            }
        }
    }
    int find(char *s,int le)
    {
        int u=0,cnt=0;
        for(int i=0;s[i];i++)
        {
            int c=idx(s[i]);
            u=ch[u][c];
            int temp=0;//必须赋初值为0,表示如果下面两个判断都不成立的时候while可以正常执行
            if(val[u])
                temp=u;
            else if(last[u])
                temp=last[u];
            while(temp)
            {
                if(height[temp]<=le) 
                    ans[temp]++;
                temp=last[temp];
            }
        }
    }
}tree;

int d[M],st[M];


const int N = 100100;
int len[N];
char *s[N];
int pos[N];
char ss[M],t1[M],t2[M];


int main()
{
    int T,n,i;
    scanf("%d",&T);
    while(T--){
        int n,m;
        tree.clear();
        memset(ans,0,sizeof(ans));
        scanf("%d%d",&n,&m);
        int del=0;
        int j=0;
        for(int i=1;i<=n;i++)
        {
            s[i]=ss+j;
            scanf(" %s",s[i]);
            len[i]=strlen(s[i])+1;
            j+=len[i];
            strcpy(ss+j,s[i]);
            ss[j-1]='z'+1;
            j+=len[i];
        }

       for(int i=1;i<=m;i++)
        {
			scanf(" %s %s",t1+1,t2);
            t1[0]='z'+1;
            strcat(t2,t1);
            pos[i]=tree.insert(t2);
        }
        tree.getfail();

        for(int i=1;i<=n;i++)
        {
            tree.find(s[i],len[i]);
        }

        for(int i=1;i<=m;i++) printf("%d\n",ans[pos[i]] );
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值