/*******************************************
功能: 查找连续重复的单词并进行统计
作者: gnehoaix
时间: 2019\4\25
********************************************/
# include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string word, last_word, same_word;
vector<int> cnt;
vector<string>cnt_word;
unsigned num = 1;
while (cin >> word)
{
if (word == last_word)
{
++num;
same_word = word;
}
else if (same_word != word && num > 1) // 重复单词向下一个不重复单词切换时,记录之前重复的单词
{
cnt_word.push_back(same_word);
cnt.push_back(num);
num = 1; // 记录完重复单词后,复位
}
last_word = word; // 存放本次输入单词,为了能为下一单词进行比较
}
if (num > 1) // 记录最后输入的重复单词
{
cnt_word.push_back(same_word);
cnt.push_back(num);
}
else if (cnt.empty())
cout << "没有重复的单词" << endl;
/*输出重复的单词和对应的数量*/
cout << "重复的单词为:" << endl;
for (size_t i = 0; i != cnt_word.size(); ++i)
{
cout << cnt_word[i] << ": " << cnt[i] << endl;
}
/*输出最大重复的单词和次数*/
size_t max = 0; // 最大重复次数
size_t it = 0; // 最大重复次数对应的序号
vector<int>::iterator j = cnt.begin();
for (; j != cnt.end(); ++j)
{
if (*j > max)
{
max = *j;
}
}
cout << endl;
cout << "最大重复的单词为:" ;
for (; it != cnt.size(); ++it)
{
if (cnt[it] == max)
{
cout << cnt_word[it] << ": " << max << endl;
}
}
return 0;
}
查找连续重复的单词并进行统计.cpp
猜你喜欢
转载自blog.csdn.net/qq_36412427/article/details/89504215
今日推荐
周排行