Sicily 1194. Message Flood 解题报告
题目:
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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 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.
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
思路:
也是很容易TEL的一道题。要把所有朋友的名字存进容器中,然后把已经发过的姓名从容器中删除,这样剩下的名字个数就是还要发的信息数。
这样要用到容器的插入和清除某个特定元素功能,而其实不需要排序等。因此考虑用set容器,其中有insert和erase函数。
名字对大小写不敏感,全部转化为小写来操作。
1 #include<iostream> 2 #include<algorithm> 3 #include<set> 4 #include<string> 5 using namespace std; 6 7 void to_lower(string &s);//大小写不敏感所以全部转化为小写 8 9 int main(){ 10 int num_of_friends,num_of_messages_received; 11 while(cin>>num_of_friends&&num_of_friends!=0){ 12 string name; 13 set<string>names_to_be_sent; 14 cin>>num_of_messages_received; 15 for(int i=0;i<num_of_friends;i++){//朋友的名字全部装进容器中 16 cin>>name; 17 to_lower(name); 18 names_to_be_sent.insert(name); 19 } 20 for(int i=0;i<num_of_messages_received;i++){ 21 cin>>name; 22 to_lower(name);//找到特定的朋友名字从容器中删除,同名再次出现时不用删除元素 23 names_to_be_sent.erase(name); 24 } 25 cout<<names_to_be_sent.size()<<endl; 26 } 27 return 0; 28 } 29 void to_lower(string &s){ 30 int len=s.length(); 31 for(int i=0;i<len;i++){ 32 if(s[i]>='A'&&s[i]<='Z'){ 33 s[i]='a'+(s[i]-'A'); 34 } 35 } 36 }