HDU6096 string(字典树)

来源:互联网 发布:一键越狱软件 编辑:程序博客网 时间:2024/06/03 20:11

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
14 4abacdeacdefacdefa acd efac ace f
 

Sample Output
2110
 

Source
2017 Multi-University Training Contest - Team 6 
 

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);        }    }}



原创粉丝点击