hdu 6096 String 字典树

来源:互联网 发布:上海交通卡充值软件下载 编辑:程序博客网 时间:2024/05/21 18:20

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

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
T≤5
0< N,Q≤100000
∑Si+Pi≤500000
∑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

#include<bits/stdc++.h>using namespace std;const int N = 5e5+100;char s[N],t[N],st[N*2];int n,q;struct Tire{    int L,root,net[N*2][26],ed[2*N];    vector<int> G[2*N];    int newnode(){        for(int i= 0;i < 26;i ++) net[L][i] = -1;        ed[L] = 0;        return L++;    }    void init(){        L = 0;        for(int i= 0;i < 2*N;i ++) G[i].clear();        root = newnode();    }    void build(char *s,int len){        //cout << s << endl;        int lens = strlen(s);        int now = root;        for(int i=0 ;i < lens;i ++){            int id = s[i]-'a';            if(net[now][id] == -1){                net[now][id] = newnode();            }            now = net[now][id];            G[now].push_back(len);            ed[now]++;        }    }    void dfs(int x){        sort(G[x].begin(),G[x].end());        for(int i = 0;i < 26;i ++){            if(net[x][i] != -1) dfs(net[x][i]);        }    }}tire;int main(){    int T;    cin >> T;    while(T--){        tire.init();        scanf("%d %d",&n,&q);        for(int i= 1;i <= n;i ++){            scanf("%s",&s);            int len = strlen(s);            for(int i= 0;i < len;i ++){                st[i*2] = s[i];                st[i*2+1] = s[len-1-i];            }            st[len*2] = '\0';            tire.build(st,len);        }        tire.dfs(0);        for(int i= 1;i <= q;i ++){            scanf("%s %s",s,t);            int len1 = strlen(s);            int len2 = strlen(t);            int ret = len1+len2;            int len = max(len1,len2);            for(int j= 0;j < len;j ++){                if(j < len1) st[j*2] = s[j];                else st[j*2] = '*';                if(j < len2) st[j*2+1] = t[len2-1-j];                else st[j*2+1] = '*';            }            len*=2;            st[len] = '\0';            //cout <<"!!!" << st << endl;            queue<int> q[2];            int tmp = 0;            q[0].push(0);            int ans = 0;            for(int j= 0;j < len;j ++){                tmp = 1-tmp;                int id = st[j]-'a';                while(!q[1-tmp].empty()){                    int now = q[1-tmp].front();                    q[1-tmp].pop();                    if(st[j] == '*'){                        for(int k = 0;k < 26;k ++){                            if(tire.net[now][k] != -1){                                q[tmp].push(tire.net[now][k]);                                int nex = tire.net[now][k];                            }                        }                    }                    else{                        if(tire.net[now][id] != -1){                            q[tmp].push(tire.net[now][id]);                            int nex = tire.net[now][id];                        }                    }                }            }            while(!q[tmp].empty()){                int now = q[tmp].front();                q[tmp].pop();                int cnt = lower_bound(tire.G[now].begin(),tire.G[now].end(),ret) - tire.G[now].begin();                ans -= cnt;                ans += tire.ed[now];            }            printf("%d\n",ans);        }    }    return 0;}