Message Flood

来源:互联网 发布:linux解压缩文件夹命令 编辑:程序博客网 时间:2024/06/04 18:13
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 3InkfishHenryCarpMaxJerichoCarpMaxCarp0

示例输出

3

 

这个题是字典树的做法,其中要防止超出内存,所以要释放内存,还有可以优化的地方,让第二组字符串建树,然后让第一组去检索树,如果检索到相同的,就返回值0,如果没有,就返回值1;

 

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>char str1[21000][15],str2[15];struct node{    int flag;    struct node *next[26];};struct node *creat(){    int i;    struct node *p;    p=new node;    for(i=0; i<26; i++)    {        p->next[i]=NULL;    }    p->flag=0;    return p;}void Insert(struct node *p,char *s){    int i,n,num;    n=strlen(s);    for(i=0; i<n; i++)    {        if(s[i]>='A'&&s[i]<='Z') num=s[i]-'A';        else if(s[i]>='a'&&s[i]<='z') num=s[i]-'a';        if(p->next[num]==NULL) p->next[num]=creat();        p=p->next[num];    }    p->flag=1;}int Search(struct node *p,char *s){    int i,n,num;    n=strlen(s);    for(i=0; i<n; i++)    {        if(s[i]>='A'&&s[i]<='Z') num=s[i]-'A';        else if(s[i]>='a'&&s[i]<='z') num=s[i]-'a';        if(p->next[num]==NULL) return 1;        p=p->next[num];    }    if(p->flag==1)    {        p->flag=0;        return 0;    }    return 1;}void Delete(struct node*p){    int i;    for(i=0; i<26; i++)    {        if(p->next[i]!=NULL)            Delete(p->next[i]);    }    free(p);}int main(){    int i,n,m;    struct node *p;    while(~scanf("%d",&n)&&n)    {        scanf("%d",&m);        p=creat();        for(i=0; i<n; i++)        {            scanf("%s",str1[i]);        }        for(i=0; i<m; i++)        {            scanf("%s",str2);            Insert(p,str2);        }        int s=0;        for(i=0;i<n;i++)        {            if(Search(p,str1[i]))                ++s;        }        printf("%d\n",s);        Delete(p);    }    return 0;}


 

0 0
原创粉丝点击