1
/**//*
2
編寫一個(gè)小程序,從標(biāo)準(zhǔn)輸入讀入一系列 string 對(duì)象,尋找連續(xù)重復(fù)出現(xiàn)的單詞。
3
程序應(yīng)該找出滿足以下條件的單詞的輸入位置:該單詞的后面緊跟著再次出現(xiàn)自己本身。
4
跟蹤重復(fù)次數(shù)最多的單詞及其重復(fù)次數(shù)。輸出重復(fù)次數(shù)的最大值,若沒有單詞重復(fù)則輸出說明信息。
5
例如,如果輸入是:
6
7
how, now now now brown cow cow
8
9
則輸出應(yīng)表明“now”這個(gè)單詞出現(xiàn)了三次。
10
11
*/
12
#include <iostream>
13
#include <string>
14
15
using namespace std;
16
17
int main()
18

{
19
string bufString = "",word = "",Aword("");
20
int count1 = 0,count2 = 0;
21
while(getline(cin,bufString))
22
{
23
if(bufString == word)
24
{
25
count1++;
26
word = bufString;
27
}
28
else
29
{
30
31
if(count1>count2)
32
{
33
count2 = count1;
34
Aword = word;
35
}
36
word = bufString;
37
count1 = 1;
38
}
39
}
40
cout << "Aword:" << Aword << " times:" << count2 << endl;
41
return 0;
42
}

/**//*2
編寫一個(gè)小程序,從標(biāo)準(zhǔn)輸入讀入一系列 string 對(duì)象,尋找連續(xù)重復(fù)出現(xiàn)的單詞。3
程序應(yīng)該找出滿足以下條件的單詞的輸入位置:該單詞的后面緊跟著再次出現(xiàn)自己本身。4
跟蹤重復(fù)次數(shù)最多的單詞及其重復(fù)次數(shù)。輸出重復(fù)次數(shù)的最大值,若沒有單詞重復(fù)則輸出說明信息。5
例如,如果輸入是:6

7
how, now now now brown cow cow8

9
則輸出應(yīng)表明“now”這個(gè)單詞出現(xiàn)了三次。10

11
*/12
#include <iostream>13
#include <string>14

15
using namespace std;16

17
int main()18


{19
string bufString = "",word = "",Aword("");20
int count1 = 0,count2 = 0;21
while(getline(cin,bufString))22

{23
if(bufString == word)24

{25
count1++;26
word = bufString;27
}28
else29

{30
31
if(count1>count2)32

{33
count2 = count1;34
Aword = word;35
}36
word = bufString;37
count1 = 1;38
}39
}40
cout << "Aword:" << Aword << " times:" << count2 << endl;41
return 0;42
}
