HDU【2846】Repository

Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3457    Accepted Submission(s): 1293


Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
 

Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
 

Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
 

Sample Input
   
   
   
   
20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s
 

Sample Output
   
   
   
   
0 20 11 11 2
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int maxnode = 500000;
const int sigma_size = 26;
struct Trie
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int flag[maxnode];
    int sz;
    Trie()
    {
        sz = 1;
        memset(ch[0],0,sizeof(ch[0]));
    }
    int idx(char c)
    {
        return c - 'a';
    }
    void Insert(char *s,int log)
    {
        int i,j;
        int n = strlen(s);
        for(i = 0 ; i < n; i++)//将字符串的子串插入字典树中
        {
            int u = 0;
            for(j = i ; j < n; j++)
            {
                int c = idx(s[j]);
                if(!ch[u][c])
                {
                    memset(ch[sz],0,sizeof(ch[sz]));
                    val[sz] = 1;
                    flag[sz] = log;
                    ch[u][c] = sz++;
                    u = ch[u][c];
                }
                else
                {
                    u = ch[u][c];
                    if(flag[u] != log)  //如果没有被标记则该节点下面的分值加1
                    {
                        flag[u] = log;
                        val[u]++;
                    }
                }
            }
        }
    }
    int Inquire(char *s)
    {
        int i;
        int u = 0;
        for(i = 0 ; i < strlen(s) ; i++)
        {
            int c = idx(s[i]);
            if(ch[u][c])
                u = ch[u][c];
            else return 0;
        }
        return val[u];
    }
};

char s[22];
Trie tree;
int main()
{
    int T;
    scanf("%d",&T);
    for(int i = 1 ; i <= T; i++)
    {
        scanf("%s",s);
        tree.Insert(s,i);
    }
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        printf("%d\n",tree.Inquire(s));
    }
    return 0;
}

你可能感兴趣的:(HDU【2846】Repository)