Write a program to read strings from standard input looking for duplicated words. The program should find places in the input where one word is followed immediately by itself. Keep track of the largest number of times
a single repetition occurs and which word is repeated.
Print the maximum number of duplicates, or else print a message saying that no word was repeated. For example, if the input is
how now now now brown cow cow
the output should indicate that the word now occurred three times.
string s;
vector<string> vs;while (cin>>s)
{
vs.push_back(s);
}
auto beg=vs.begin();
string prevStr=*beg;
string mostStr;
int maxCount=0,count=0;
for (;beg!=vs.end() ;++beg )
{
if (prevStr==*beg)
{
++count;
}
else
count=1;
if (count>maxCount)
{
maxCount=count;
mostStr=prevStr;
}
prevStr=*beg;
}
cout<<maxCount<<" times "<<mostStr<<" occurs"<<endl;
本博客介绍了一个程序,用于从标准输入读取字符串并查找重复的单词。程序会跟踪并显示单个重复出现的最大次数及对应的单词。
696

被折叠的 条评论
为什么被折叠?



