trie树Message Flood

来源:互联网 发布:三星电话交换机编程 编辑:程序博客网 时间:2024/05/17 03:14

Message Flood

Time Limit: 1500MS Memory limit: 65536K

题目描述

Well, how do you feel about mobile phone? Your answer wouldprobably be something like that "It's so convenient and benefitspeople a lot". However, If you ask Merlin this question on the NewYear's Eve, he will definitely answer "What a trouble! I have tokeep my fingers moving on the phone the whole night, because I haveso many greeting message to send!" Yes, Merlin has such a long namelist of his friends, and he would like to send a greeting messageto each of them. What's worse, Merlin has another long name list ofsenders that have sent message to him, and he doesn't want to sendanother message to bother them Merlin is so polite that he alwaysreplies each message he receives immediately). So, before he beginsto send message, he needs to figure to how many friends are left tobe sent. Please write a program to help him. Here is something thatyou should note. First, Merlin's friend list is not ordered, andeach name is alphabetic strings and case insensitive. These namesare guaranteed to be not duplicated. Second, some senders may sendmore than one message to Merlin, therefore the sender list may beduplicated. Third, Merlin is known by so many people, that's whysome message senders are even not included in his friendlist.

输入

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

输出

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

示例输入

5 3 
Inkfish 
Henry 
Carp 
Max 
Jericho 
Carp 
Max 
Carp 
0

示例输出

3
认真理解题意
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int t;
struct node
{
    int flag;
    struct node *next[26];
};
struct node *chuangjian()
{
     int i;
     struct node *p;
     p=(struct node*)malloc(sizeof(struct node));
     p->flag=0;
     for(i=0;i<26;i++)
         p->next[i]=NULL;
     return p;
}
void insert(char *s,struct node *root)
{
     int i;
     struct node *p;
     p=(struct node*)malloc(sizeof(struct node));
     p->flag=0;
     p=root;
     int len=strlen(s);
     for(i=0;i<len;i++)
     {
         if(p->next[s[i]-'a']==NULL)
         {
             p->next[s[i]-'a']=chuangjian();
         }
         p=p->next[s[i]-'a'];
     }
     p->flag=1;
}
void search(char *s,struct node *root)
{
     int i,n;
     n=strlen(s);
     struct node *p;
     p=root;
     for(i=0;i<n;i++)
     {
         if(p->next[s[i]-'a']!=NULL)            
          p=p->next[s[i]-'a'];
     }
      if(p->flag)
    {
        p->flag=0;
        t--;
    }
    
}
void change(char *s)
{
    int len, i;
    len = strlen(s);
    for(i=0;i<len;i++)
        if(s[i]>='A'&&s[i]<='Z')
            s[i]+=32;
}
int main()
{
     int n,m;
     char str[100];    
     while( scanf("%d",&n),n)
     {
         t=n;
         struct node *head;
        head=chuangjian();        
         scanf("%d",&m);            
        while(n--)
        {
           scanf("%s",str);
             change(str);
           insert(str,head);
        }
      
        while(m--)
        {
           scanf("%s",str);
            change(str);
           search(str,head);
        }
          printf("%d\n",t);
     }
        
     return 0;
}