SDUT 1500 Message Flood
Message Flood
Time Limit: 1500 ms Memory Limit: 65536 KiB
Problem Description
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.
Input
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.
Output
For each case, print one integer in one line which indicates the number of left friends he must send.
Sample Input
5 3 Inkfish Henry Carp Max Jericho Carp Max Carp 0
Sample Output
3
提示:这个题用到了哈希,题意就是查看去除输入的m个数据,还剩多少个联系人需要回复。
至于字符串的哈希,推荐用C++中的map函数,方便快捷,map充当一个容器,也相当于一个一对一的哈希表,所以用来做这个题来再合适不过。
代码实现如下(g++):
#include <bits/stdc++.h> #include <map> using namespace std; int main() { int n,m,i,z,l,j; char str[20001][10]; char k[10]; int a[20001]; memset(a,0,sizeof(a)); map<string,int>mp;//建立map容器的标准写法 while(~scanf("%d",&n)&&n!=0) { scanf("%d",&m); for(i=0;i<n;i++) { scanf("%s",str[i]); l=strlen(str[i]); for(j=0;j<l;j++) { if(str[i][j]>='A'&&str[i][j]<='Z') str[i][j]+=32; } mp[str[i]]=1;//把所有的联系人对应的值变为1 } for(i=0;i<m;i++) { scanf("%s",k); l=strlen(k); for(j=0;j<l;j++) { if(k[j]>='A'&&k[j]<='Z') k[j]+=32; } if(mp[k]==1)//如果此联系人存在于列表中,将其置为0 mp[k]=0; } for(i=0,z=0;i<n;i++) { if(mp[str[i]]==1)//看还剩多少需要发送的人 z++; } printf("%d\n",z); } return 0; } /*************************************************** Result: Accepted Take time: 244ms Take Memory: 10144KB ****************************************************/