1194. Message Flood

1194. Message Flood

Total: 23747 Accepted: 4451 Rating:
3.1/5.0(78 votes)
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
                                        Copy sample input to clipboard                                   
5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0
Sample Output
3

Problem Source: ZSUACM Team Member

关键:

set容器

注意:

1、输入一个0表示结束输入,这地方容易出错。

这时候不能单纯的用

while(cin>>n>>m)

{...}

这样输出的结果是错的。

这里需要用到

while(true)

{

if(cin>>n)    

break;   

cin>>m;

....

}

2、每个例子结束的时候,记得要清空容器,否则上次的结果还保留在容器中。

3、字符串大小写转换。注意本题是不区分大小写的,这个一定要注意。

如何将一个字符串转换成大写或者小写?这是字符串匹配中经常需要做的事情,然而C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写(toupper)和小写(tolower)的功能而已。 但我们可以利用STL的transform配合toupper/tolower,完成std::string转换大(小)写的功能,也看到 模版编程 的威力了,一个transform函数,可以适用于任何类型,且只要自己提供 函数 ,就可完成任何Transform的动作。

C++

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
int main() {
string s = "Clare";
// toUpper
transform(s.begin(), s.end(), s.begin(), ::toupper);
// toLower
//transform(s.begin(),s.end(),s.begin(), ::tolower);
cout << s << endl;
}

C

#include <stdio.h>
#include <ctype.h>
int main() {
char s[] = "Clare";
int i = -1;
while(s[i++])
s[i] = toupper(s[i]);
// s[i] = tolower(s[i]);
puts(s);
}

或者用最原始的方法,每输入一个字符串,则进行一次循环:

string rName="Alexander";

for(j = 0; j < rName.length(); j++)

                rName[j] = tolower(rName[j]);

我的答案:

View Code
#include<iostream>
#include<fstream>
#include<set>
#include <cctype>
#include<string>
#include <algorithm>
using namespace std;

int main()
{
int n=0,m=0;
string strFriend,sender;
set<string> friends;
// ifstream cin("input.txt");
set<string>::iterator it;
while(true)
{
if(cin>>n)
break;
cin>>m;
for(int i=0;i<n;++i)
{
cin>>strFriend;
transform(strFriend.begin(), strFriend.end(), strFriend.begin(), ::toupper);
friends.insert(strFriend);
}

for(int j=0;j<m;++j)
{
cin>>sender;
transform(sender.begin(), sender.end(), sender.begin(), ::toupper);
it=friends.find(sender);
if(it!=friends.end())
{
friends.erase(it);
}
}

cout<<friends.size()<<endl;
friends.clear();
}

return 0;
}

用时 大小:

0.25s 1368KB


原文地址:https://www.cnblogs.com/wangchunming/p/2397156.html