HDU1251 (statistical conundrum) (trie template title

Statistical problems

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 69587    Accepted Submission(s): 24087


Problem Description
Ignatius recently encountered a problem, the teacher gave him a lot of words (only lowercase letters, there will be no repetition of the word appears), and now the teacher asked him to count out a string prefixed with the number of words (the word itself is its own prefix).
 
Input
The first part of the input data is a list of words, one word per line, the word length of not more than 10, they represent a statistics teacher to Ignatius words, a blank line represents the word of the table. The second part is a series of questions, one per line questions, each question is a string.

Note: this title is only a set of test data, the processing to the end of the file.
 
Output
For each question, the number given to the string is a prefix word.
 
Sample Input
banana band bee absolute acm ba b band abc
 
Sample Output
2 3 1 0
 
 

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
const int maxn=1000000;
int trie[maxn][27];
int ojbk[maxn];
int color[maxn];
int k=1;
using namespace std;
void _insert(char *w)
{
    int len=strlen(w);
    int p=0;//节点.
    for(int i=0;i<len;i++)
    {
        int temp=w[i]-'a';
        if(!trie[p][temp])
        {
            trie[p][temp]=k++;
        }
        p=trie[p][temp];
        ojbk[p]++;
    }
    color[p]=1;
}
int query(char *s)
{
    int len=strlen(s);
    int ans=0;
    int p=0;
    for(int i=0;i<len;i++)
    {
        int temp=s[i]-'a';
        if(!trie[p][temp])
        {
            return 0;
        }
        p=trie[p][temp];
    }
    return ojbk[p];
}
int main()
{
    char word[15];
    memset(ojbk,0,sizeof ojbk);
    memset(color,0,sizeof color);
    while(gets(word))
    {
        int len1=strlen(word);
        if(len1==0)
        break;
        _insert(word);
    }
    while(gets(word))
    {
        printf("%d\n",query(word));
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/switch-waht/p/11519435.html