hdu 6096

String

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 702    Accepted Submission(s): 225


Problem Description
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 )
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 )
All of the above characters are lowercase letters.
The dictionary does not contain the same words.

Limits
T5
0<N,Q100000
Si+Pi500000
Wi500000
 

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
若主字符串为"cde",将主字符串变成"cde#cde",若前缀为"ac",后缀为"a",则变为"a#ac". 注意'#'为任意非'a'~'z'的字符即可。
然后建立AC自动机,让主串去匹配AC自动机上的前后缀串。稍微改下AC自动机模板即可。
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int MX =1e6+1e5+7;

char str[MX];
char *s[MX];
char s1[MX],s2[MX];
int ans[MX],lenth[MX];
struct AC_machine {
    int rear, root;
    int Next[MX][27], Fail[MX], End[MX], dl[MX],pos[MX];

    void Init(){
        rear = 0;
        root = New();
    }

    int New() {
        rear++;
        End[rear] = 0;
        for(int i = 0; i < 27; i++){
            Next[rear][i] = -1;
        }
        return rear;
    }

    void Add(char*A,int I) {
        int n = strlen(A), now = root;
        for(int i = 0; i < n; i++) {
            int id = A[i] - 'a';
            if(Next[now][id] == -1) {
                Next[now][id] = New();
                dl[rear] = i+1;
            }
            now = Next[now][id];
        }
        End[now] = 1;
        pos[I] = now;
    }

    void Build(){
        queue<int>Q;
        Fail[root] = root;
        for(int i = 0; i < 27; 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 u = Q.front();
            Q.pop();

            for(int i = 0; i < 27; i++) {
                if(Next[u][i] == -1){
                    Next[u][i] = Next[Fail[u]][i];
                }else{
                    Fail[Next[u][i]] = Next[Fail[u]][i];
                    Q.push(Next[u][i]);
                }
            }
        }
    }

    void Query(char *S, int len){
        int n = strlen(S), now = 1, ret = 0;
        for(int i = 0; i < n; i++) {
            now = Next[now][S[i] - 'a'];
            int temp = now;
            while(temp != root){
                if(dl[temp] <= len)
                    ans[temp]++;
                temp = Fail[temp];
            }
        }
    }
}AC;



int main()
{
    int T,n,m;
    scanf("%d",&T);
    while(T--){
        AC.Init();
        scanf("%d%d",&n,&m);
        memset(str,0,sizeof(str));
        memset(ans,0,sizeof(ans));
        int j = 0,len;
        for(int i = 1; i <= n; i++){
            s[i] = str+j;
            scanf("%s",s[i]);
            len = strlen(s[i]);
            lenth[i] = len+1;
            strcpy(s1,s[i]);
            s[i][len] = 'z'+1;

            strcat(s[i],s1);
            len = strlen(s[i]);
            s[i][len] = 0;
            j += len+1;
        }
        for(int i = 1; i <= m; i++){
            scanf("%s%s",s1,s2);
            len = strlen(s2);
            s2[len] = 'z'+1;
            strcat(s2,s1);
            AC.Add(s2,i);

            len = strlen(s1);
            for(int j = 0; j < len; j++) s1[j] = 0;
            len = strlen(s2);
            for(int j = 0; j < len; j++) s2[j] = 0;
        }

        AC.Build();
        for(int i = 1; i <= n; i++){
            AC.Query(s[i],lenth[i]);
        }
        for(int i = 1; i <= m; i++){
            printf("%d\n",ans[AC.pos[i]]);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值