-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1071.cpp
87 lines (87 loc) · 1.37 KB
/
1071.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<stdio.h>
#include<string>
#include<map>
#include<string.h>
#include<ctype.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char buffer[1048577];
gets(buffer);
map<string,int> dict;
int begin=0;
int i;
bool inword=false;
int maxFre=0;
int len=strlen(buffer);
for(i=0;i<len;i++)
{
if(!inword && isalnum(buffer[i]))
{
begin=i;
if(buffer[i]>='A' && buffer[i]<='Z')
buffer[i]=buffer[i]+'a'-'A';
inword=true;
}
else if(!inword && !isalnum(buffer[i]))
;
else if(inword &&isalnum(buffer[i]))
{
if(buffer[i]>='A' && buffer[i]<='Z')
buffer[i]=buffer[i]+'a'-'A';
}
else
{
buffer[i]='\0';
inword=false;
string s(&buffer[begin]);
map<string,int>::iterator it=dict.find(s);
if(it!=dict.end())
{
it->second=it->second+1;
if(it->second>maxFre)
{
maxFre=it->second;
}
}
else
{
dict[s]=1;
if(maxFre==0)
maxFre=1;
}
begin=i+1;
}
}
if(inword)
{
string s(&buffer[begin]);
map<string,int>::iterator it=dict.find(s);
if(it!=dict.end())
{
it->second=it->second+1;
if(it->second>maxFre)
{
maxFre=it->second;
}
}
else
{
dict[s]=1;
if(maxFre==0)
maxFre=1;
}
}
map<string,int>::iterator it;
for(it=dict.begin();it!=dict.end();it++)
{
if(it->second == maxFre)
{
break;
}
}
cout<<it->first<<' '<<maxFre;
return 0;
}