pat 1071

Attention

注意测试用例 b a b a//输出为 a 2

而当你输出为b 2的时候,说明你没有吧最后一个字符串计入哦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
//一切除了大小写字母,数字之外的字符都可以作为单词的分隔符(Ascii)
//vector<string> keys;
map<string , int> elem;
int main()
{
string s;
char ch;
while((ch = getchar()) != '\n')
{
if(ch >= 'A' && ch <= 'Z')
{
ch += 32;//转换为小写
}
if(ch < '0' || (ch > '9' && ch <'A') || (ch > 'Z' && ch < 'a') || ch >'z')
{
if(s != "")
{
if(elem.find(s) != elem.end())
elem[s]++;
else
elem[s] = 1;
}
s = "";
continue;
}
s += ch;
}
if(s != "")
{
if(elem.find(s) != elem.end())
elem[s]++;
else
elem[s] =1;
}
int maxItem = 0;
string maxKey;
for(map<string , int>::iterator it = elem.begin() ; it != elem.end() ; ++it)
{
if(maxItem < it->second)
{
maxItem = it->second;
maxKey = it->first;
}
}
cout<<maxKey<<" "<<maxItem;
}

热评文章