Message Flood
时间限制(普通/Java) :
2000 MS/ 6000 MS 运行内存限制 : 65536 KByte
总提交 : 404 测试通过 : 108
总提交 : 404 测试通过 : 108
题目描述
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 messages 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 messages , 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 pre line . After that there are m lines of alphabetic string s ,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
题目链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1121
题目大意:有n个名字不重复的朋友发来祝福消息,有m条消息已回复给对应名字的朋友,求还需要回复多少个朋友。
解题思路:字典树记录n个名字,一个完整名字插入后在最后打上标记,对应名字查找到时记录查到个数,并把标记取消,避免重复查找。最后n减去已回复的朋友个数,即为还要回复的朋友个数。
代码如下:
#include <cstdio>
#include <cstring>
const int maxn=20005;
char s[maxn];
int ans;
struct node
{
bool end;
node* next[26];
node()
{
memset(next,NULL,sizeof(next));
end=false;
}
};
void Insert(node *p,char *s)
{
for(int i=0;s[i]!='\0';i++)
{
int a; //把名字的每个字母转换成数字被当做下标记录
if(s[i] <= 'Z') //不用区分大小写时同一个字母的大小写换乘同一个数字
a = s[i] - 'A';
else
a = s[i] - 'a';
if(p->next[a]==NULL) //p的下一个下标为a的节点为空时,插入该节点
p->next[a]=new node();
p=p->next[a]; //p指针指向下一个节点
}
p->end=true; //名字插入完成时名字末尾字母节点的end标记为true
}
void Search(node *p, char *s)
{
for(int i=0;s[i]!='\0';i++)
{
int a; //把名字的每个字母转换成数字被当做下标记录
if(s[i]<='Z') //不用区分大小写时同一个字母的大小写换乘同一个数字
a=s[i]-'A';
else
a=s[i]-'a';
if(p->next[a]==NULL) //查找失败,返回
return ;
p=p->next[a]; //查找下一个节点
}
if(p->end) //找到,个数增加
ans++;
p->end=false; //为避免重复查找,标记为false,表示该串字符已不能作为名字被查找
}
int main()
{
int n,m;
while(true)
{
ans=0;
node *root=new node(); //获得根节点
scanf("%d",&n);
if(n==0)break;
scanf("%d",&m);
int nn=n;
while(nn--)
{
scanf("%s",s);
Insert(root ,s); //从跟节点开始插入名字
}
while(m--)
{
scanf("%s",s);
Search(root,s); //从根节点开始查找名字
}
printf("%d\n",n-ans);
}
return 0;
}