怪物猎人2g龙骑枪:C++ 编程的问题,麻烦各位帮我编一下,谢谢!!!
来源:百度文库 编辑:中科新闻网 时间:2024/05/09 19:47:58
how now now now brown cow cow
则输出应表明“now”这个单词出现了3次。
#include <iostream>
#include <sstream>
using namespace std;
void main( ) {
string line;
getline( cin, line );
stringstream ss( line );
string previousWord,
currentWord,
wordOfMaxCR; // CR == Consecutive Repetition
int maxCR = 0,
currentCR = 0;
while ( ss >> currentWord ) {
if ( previousWord == currentWord )
currentCR++;
else { // 检测可能的新冠军
if ( currentCR > maxCR ) {
maxCR = currentCR;
wordOfMaxCR = previousWord;
}
currentCR = 0;
}
previousWord = currentWord;
}
// 最最后一次检测可能的新冠军
if ( currentCR > maxCR ) {
maxCR = currentCR;
wordOfMaxCR = previousWord;
}
if ( maxCR )
cout << wordOfMaxCR << " 得了年度最高连续出现大奖"
"(连续出现 " << ++maxCR << " 次)。\n";
else
cout << "没有连续出现的单词。\n";
}
使用KMP算法能够快速对文本数据进行检索,尤其是文本非常长,时间能够节约80%左右