sdut 1500 Message Flood
2012-08-13 17:11
295 查看
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 "Whata 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(thelength 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
来源
第9届中山大学程序设计竞赛预选赛题意:先输入电话本n(每个人都是唯一的)个人,然后输入m(可能有重复)个人,问还有发送几个人,就是去掉m个人中的重复,n-m
思路 :字典树
#include<stdio.h> #include<string.h> #include<stdlib.h> struct node { int flag; struct node *next[26]; }; struct node *newtrie() { struct node *p; int i; p=(struct node *)malloc(sizeof(struct node)); for(i=0;i<26;i++) p->next[i]=NULL; p->flag=0; return p; } void insert(struct node *root,char s[]) { struct node *p; p=root; int i,len,t; len=strlen(s); for(i=0;i<len;i++) { if(s[i]>='A'&&s[i]<='Z') t=s[i]-'A'; else t=s[i]-'a'; if(p->next[t]==NULL) p->next[t]=newtrie(); p=p->next[t]; } p->flag=1; } int search(struct node *p,char *s) { int i,len,t; len=strlen(s); for(i=0;i<len;i++) { if(s[i]>='A'&&s[i]<='Z') t=s[i]-'A'; else t=s[i]-'a'; if(p->next[t]==NULL) return 0; p=p->next[t]; } if(p->flag==1) { p->flag=0; return 1; } return 0; } void dell(struct node *p) { int i; for(i=0;i<26;i++) { if(p->next[i]!=NULL) dell(p->next[i]); } free(p); p=NULL; } int main() { int m,n,i,j,count,len; struct node *root; char st[50]; while(scanf("%d",&n)!=EOF) { if(n==0) break; count=0; scanf("%d",&m); root=newtrie(); for(i=1;i<=n;i++) { scanf("%s",st); insert(root,st); } while(m--) { scanf("%s",st); count+=search(root,st); } printf("%d\n",n-count); dell(root); } return 0; }
相关文章推荐
- sdut 1500 Message Flood
- SDUT1500_Message Flood(字典树)
- sdut 1500 Message Flood
- SDUT 1500-Message Flood(set)
- sdut 1500 Message Flood(Trie树)
- SDUT 1500 Message Flood
- SDUT 1500-Message Flood(set)
- sdut1500 Message Flood
- SDUT 1500 Message Flood(字典树 && 模板)
- SDUT 1500 Message Flood
- 【1500】Message Flood (SDUTOJ)
- SDUT 1500 Message Flood trie树
- 【1500】Message Flood (SDUTOJ)
- SDUT-1500-(字典树)
- SDUTOJ 1500 ——Message Flood 字典树或map
- oj1500(Message Flood)字典树
- 1500_Message Flood
- BZOJ 1500[NOI2005] 维修数列
- SDUT 2151 Phone Number 2010年山东省第一届ACM大学生程序设计竞赛
- 电影节(SDUT_2797)