编写一个小程序,从标准输入读入一系列 string 对象,寻找连续重复出现的单词。程序应该找出满足以下条件的单词的输入位置:该单词的后面紧跟着再次出现自己本身。跟踪重复次数最多的单词及其重复次数。输出重复次数的最大值,若没有单词重复则输出说明信息。例如,如果输入是:
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s, stemp, sresult;
int count = 0;
int counttemp = 0;
//
//控制x,x,y,y,z,z的情况,这种情况不会被判为重复
int initial = 0;
int i = 0;
//
while (cin >> s)
{
//string stemp;
if (stemp.empty())
{
stemp = s;
counttemp = 1;
count = 1;
sresult = s;
//initial = 1;
cout << "初始化各值" << endl;
//sresult=s;
}
else if (stemp == s)
{
++counttemp;
//++initial;
//++count;
cout << "与上一次一样" << endl;
cout << "couttemp=" << counttemp << endl;
//cout << "initial=" << initial << endl;
if (count<counttemp)
{
count = counttemp;
sresult = stemp;
cout << "count=" << count << endl;
}
}
else
{
cout << "与上一次不一样" << endl;
//sresult=stemp;
/* if (count<counttemp)
{
count = counttemp;
sresult = stemp;
cout << "count=" << count << endl;
}*/
stemp = s;
counttemp = 1;
while (i != 1)
{
initial = count;
i++;
}
}
//else
//{
// if (count<counttemp)
// {
// count = counttemp;
// sresult = stemp;
// }
// //stemp=s;
// //counttemp=0;
//}
}
/*if (flag==0)
{
cout << "count:" << counttemp << "string:" << sresult << endl;
}
else if (flag==1)
{
cout << "count:" << count << "string:" << sresult << endl;
}*/
if (initial == count)
{
cout << "没有元素过于重复" << endl;
}
else
cout << "count:" << count << "string:" << sresult << endl;
system("pause");
return 0;
}