-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExer05_14.cpp
39 lines (39 loc) · 988 Bytes
/
Exer05_14.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
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
// What if two words repeated the same times?
// Consider this situation: in a challenge game, if you want to be a new
// champion, you have to do even better than the last one. Just doing as
// well as him/her is not enough. So the champion is always the first that
// surmounts the others.
string s, sLast, sMax;
int cntNow = 0, cntMax = 0;
while(cin >> s)
{
if(s == sLast)
++cntNow;
else
cntNow = 1;
if(cntNow > cntMax)
{
sMax = s;
cntMax = cntNow;
}
sLast = s;
}
if(!sMax.empty())
{
if(cntMax > 1)
cout << sMax << " occurred " << cntMax << " times." << endl;
else
cout << "No duplicated words." << endl;
}
else
cout << "Nothing is input!" << endl;
return 0;
}