从标准输入读入一系列string对象,寻找连续重复出现的单词
程序应该找出满足以下条件的单词的输入位置:该单词的后面紧跟着再次出现自己本身,跟踪重复次数多的单词及其重复次数.输出重复次数的最大值。
没错,C++ 吧里有人问的,我觉得如果只是单纯的想知道重复次数最多的单词,可以这样:
void foo(){ string input; string current; string maxStr; size_t max = 1; size_t count = 1; bool init = false; while (cin >> input){ if (!init){ maxStr = current = input; init = true; } else if (input == current){ ++count; } else if(count > max){ maxStr = current; max = count; current = input; count = 1; } else{ current = input; count = 1; } } cout << maxStr << " " << max;}