hdu 6096 String 字典树

本文介绍了一种利用Trie树解决特定字符串匹配问题的算法,该问题涉及通过前缀和后缀来确定字典中可能匹配的单词数量。文章详细阐述了算法的实现过程,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值