hdu 6096 String

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
讲道理,这题a了是a了,但是我不会证明它的时间复杂度,之所以敢去写,是因为挂机也是挂机,不如先写写,不行再慢慢优化,因为我感觉这题造数据大概不好造把。
首先建立字典树,对于串s,是把s串第0位,第n-1位,第1位,第n-2位,,,这样的顺序插入字典树的,然后记录每个节点开一个vector,保存这个经过这个点的所有串的长度,因为每个字符串对vector的贡献是它的长度,所以vector的最大长度也是5e5的,所以空间是不会爆的。
然后查询的时候,把前后缀按照字典树的构造方法构造,如果前后缀长度不同,就‘*’补全,然后用两个queue转移合法的点,最后对每个合法的点,去掉前后缀会重叠的情况,就可以了。

#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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值