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
2017 Multi-University Training Contest - Team 6 
 

Recommend
liuyiding
 

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

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




#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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;iy.len;
}
int cmp1(node x,node y){
    return x.id=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



你可能感兴趣的:(算法,字符串)