编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足一下条件的单词:该单词的后面紧接着再次出现自己本身。跟踪重复次数最多的单词及其重复次数,输出.
// test13.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> vec;
vector<int> strCnt;
vector<string> strName;
string str;
string temp;
int num = 0;
int max=0,flag=0;
while (cin>>str)
{
vec.push_back(str);
str.clear();
if (getchar() == '\n')
{
break;
}
}
for (auto it = vec.begin(); it != vec.end();it++)
{
cout << *it << endl;
}
temp = *vec.begin();
for (auto it = vec.begin(); it != vec.end(); it++)
{
if (it + 1 == vec.end())
{
num++;
strName.push_back(temp);
strCnt.push_back(num);
}
else if (temp == *it)
num++;
else
{
strName.push_back(temp);
strCnt.push_back(num);
temp = *it;
num = 1;
}
}
for (int i = 0; i < strName.size();i++)
{
cout << strName[i] << ":" << strCnt[i] << endl;
}
max = *strCnt.begin();
for (int i=0; i<strCnt.size(); i++)
{
if (max < strCnt[i])
{
max = strCnt[i];
flag = i;
}
}
cout << "连续出现的字符串的名称是:" << strName[flag] << " " <<"出现次数是:"<< strCnt[flag] << endl;
}