HDU6096 string(字典树)

String

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


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
 

Source
 

Recommend
liuyiding
 

题意:给n个单词的字典,每次查询给出两个单词,表示一个单词的前缀和后缀,问有多少种匹配的可能。

思路:将每个单词按照第1个字母,第n个字母,第2个字母第n-1个字母这样的顺序插入。每个单词插入2n个字母。查询的时候也按相应的顺序查询,对于后缀前缀长度不够的情况用$替代,字典树遇到*则查询所有字母。




#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
const int MAXN=500000+10;
struct trie{
    int ch[MAXN<<3][30];
    int val[MAXN<<3];
    int sz;
    void init(){
        sz=1;
        memset(ch[0],0,sizeof ch[0]);
        memset(val,0,sizeof val);
    }
    int idx(char c){
        return c-'a';
    }
    void insert(const char *s){
        int u=0,n=(int)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]);
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
            val[u]++;
        }
    }
    int query(const char *s,int rt,int n,int cur){
        int res=0;
        if(cur==n){
            return val[rt];
        }
        if(s[cur]=='$'){
            for(int i=0;i<26;i++){
                if(ch[rt][i]){
                    res+=query(s, ch[rt][i], n, cur+1);
                }
            }
        }else{
            int c=idx(s[cur]);
            if(ch[rt][c]){
                return query(s,ch[rt][c],n,cur+1);
            }
            return 0;
        }
        return res;
    }
};
char s[MAXN<<1],t[MAXN<<1];
struct node{
    string s;
    int len;
    int cnt;
    int id;
    int res;
}a[MAXN],b[MAXN];
int cmp(node x,node y){
    return x.len>y.len;
}
int cmp1(node x,node y){
    return x.id<y.id;
}
int T;
trie tr;
int main(){
    //freopen("/Users/cbox/QQ/第六次多校标程与数据/数据/1001.in","r",stdin);
    scanf("%d",&T);
    while(T--){
        int n,q;
        tr.init();
        scanf("%d%d",&n,&q);
        for(int i=0;i<n;i++){
            scanf("%s",s);
            a[i].s="";
            int len=(int)strlen(s);
            for(int j=0;j<len;j++){
                a[i].s+=s[j];
                a[i].s+=s[len-j-1];
            }
            a[i].len=len;
        }
        //cout<<a[0].s<<endl;
        sort(a,a+n,cmp);
        for(int i=0;i<q;i++){
            scanf("%s%s",s,t);
            int l1=(int)strlen(s);
            int l2=(int)strlen(t);
            int L=max(l1,l2);
            b[i].s="";
            int x=0,y=0;
            for(int j=0;j<L;j++){
                if(x<l1){
                    b[i].s+=s[x++];
                }else{
                    b[i].s+='$';
                }
                if(y<l2){
                    b[i].s+=t[l2-y-1];
                    y++;
                }else{
                    b[i].s+='$';
                }
            }
            b[i].len=l1+l2;
            b[i].cnt=2*L;
            b[i].id=i;
        }
        sort(b,b+q,cmp);
        int temp=0;
        for(int i=0;i<q;i++){
            int l=b[i].len;
            while(temp<n&&a[temp].len>=l){
                tr.insert(a[temp].s.c_str());
                temp++;
            }
            b[i].res=tr.query(b[i].s.c_str(),0,b[i].cnt,0);
        }
        sort(b,b+q,cmp1);
        for(int i=0;i<q;i++){
            printf("%d\n",b[i].res);
        }
    }
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值