字典树

迷之好奇

Time Limit: 2000MS Memory limit: 65536K

题目描述

FF得到了一个有n个数字的集合。不要问我为什么,有钱,任性。

FF很好奇的想知道,对于数字x,集合中有多少个数字可以在x前面添加任意数字得到。

如,x = 123,则在x前面添加数字可以得到4123,5123等。

输入

  多组输入。

对于每组数据

首先输入n(1<= n <= 100000)

接下来n行。每行一个数字y(1 <= y <= 100000)代表集合中的元素。

接下来一行输入m(1 <= m <= 100000),代表有m次询问。

接下来的m行。

每行一个正整数x(1 <= x <= 100000)

输出

  对于每组数据,输出一个数字代表答案。

示例输入

3
12345
66666
12356
3
45
12345
356

示例输出

1
0
1

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define LL long long
using namespace std;
struct node
{
    int flag;
    int End;
    int next[26];
}p[610000];
int top;
int Creat()
{
    memset(p[top].next,-1,sizeof(p[top].next));
    p[top].flag=0;
    return top++;
}
int Build(char s[])
{
    int k=0;
    int len=strlen(s);
    for(int i=len-1;i>=0;i--)
    {
        int ch = s[i]-'0';
        if(p[k].next[ch]==-1)
            p[k].next[ch]=Creat();
        k=p[k].next[ch];
        p[k].flag++;
    }
    p[k].flag--;
}
int Find(char s[])
{
    int k=0;
    int len=strlen(s);
    for(int i=len-1;i>=0;i--)
    {
        int ch = s[i]-'0';
        if(p[k].next[ch]==-1)
            return 0;
        k=p[k].next[ch];
    }
    return p[k].flag;
}
char s[100];
int main()
{
    int n,m;
    while(~scanf("%d",&n))
    {
        top=0;
        int k=Creat();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            Build(s);
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            printf("%d\n",Find(s));
        }
    }
    return 0;
}

Message Flood

Time Limit: 1500MS Memory limit: 65536K

题目描述

Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so many greeting message to send!" Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that have sent message to him, and he doesn't want to send another message to bother them Merlin is so polite that he always replies each message he receives immediately). So, before he begins to send message, he needs to figure to how many friends are left to be sent. Please write a program to help him. Here is something that you should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. These names are guaranteed to be not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known by so many people, that's why some message senders are even not included in his friend list.

输入

There are multiple test cases. In each case, at the first line there are two numbers n and m (1<=n,m<=20000), which is the number of friends and the number of messages he has received. And then there are n lines of alphabetic strings(the length of each will be less than 10), indicating the names of Merlin's friends, one per line. After that there are m lines of alphabetic strings, which are the names of message senders. The input is terminated by n=0.

输出

For each case, print one integer in one line which indicates the number of left friends he must send.

示例输入

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0

示例输出

3
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define LL long long
using namespace std;
struct node
{
    int flag;
    int End;
    int next[26];
}p[610000];
int top;
int Creat()
{
    memset(p[top].next,-1,sizeof(p[top].next));
    p[top].flag=0;
    return top++;
}
int Build(char s[])
{
    int k=0;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int ch;
        if(s[i]>='a'&&s[i]<='z')
        ch = s[i]-'a';
        else
        ch =s[i]-'A';
        if(p[k].next[ch]==-1)
            p[k].next[ch]=Creat();
        k=p[k].next[ch];
    }
    p[k].flag=1;
}
int Find(char s[])
{
    int k=0;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int ch;
        if(s[i]>='a'&&s[i]<='z')
        ch = s[i]-'a';
        else
        ch =s[i]-'A';
        if(p[k].next[ch]==-1)
            return 0;
        k=p[k].next[ch];
    }
    if(p[k].flag)
    {
        p[k].flag=0;
        return 1;
    }
    else
    return 0;
}
char s[100];
int main()
{
    int n,m;
    while(~scanf("%d",&n))
    {
        if(!n)
            break;
        scanf("%d",&m);
        top=0;
        int k=Creat();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            Build(s);
        }
        int sum=0;
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            ///printf("%d\n",Find(s));
            sum+=Find(s);
        }
        printf("%d\n",n-sum);
    }
    return 0;
}



你可能感兴趣的:(字典树)