<Span style = "font-family:, Arial, Helvetica, san-serif;"> question: </span> write a small program to read a series of string objects from standard input, to find the words that appear repeatedly consecutively, the program should find the input position of the words that meet the following conditions: the word is followed by itself, the number of repeated words and the number of repeated times are traced. maximum number of output repetitions, for example. if the input is: how now brown cow, the output indicates that the word now appears three times.
My solution:
# Include <iostream> # include <string> # include <cctype> # include <vector> using namespace std; int main () {<pre name = "code" class = "cpp"> <span style = "white-space: pre"> </span> string s; vector <string> vec; int maxRepeat = 0; while (cin> s) {if (ispunct (s [s. size ()-1]) s = s. substr (0, s. size ()-1); // remove the final punctuation mark vec. push_back (s);} vector <string >:: size_type I = 0; vector <int> vecInt (vec. size (), 1); whi Le (I! = Vec. size ()-1) {int j = I; string str = vec [I]; while (I! = Vec. size ()-1) {if (str = vec [++ I]) vecInt [j] ++; else break ;}} vector <int> :: size_type k = 0; int max = vecInt [k]; int flag = 0; while (k! = VecInt. size ()-1) {if (max <vecInt [++ k]) {max = vecInt [k]; flag = k ;}} cout <"The word of" <vec [flag] <"repeats:" <vecInt [flag] <"times. "<endl; cout <endl; </pre> <pre name = "code" class = "cpp"> <pre name = "code" class = "cpp"> return 0 ;} </pre> <p> the solution is correct, but it is much more complicated in terms of space complexity, so I searched the internet and found another solution. The modified code is as follows: </p> <pre name = "code" class = "cpp"> # include <iost Ream> # include <string> # include <cctype> using namespace std; int main () {</pre> <pre name = "code" class = "cpp"> <pre name = "code" class = "cpp"> <span style = "white-space: pre "> </span> string nowWord, beforeWord, result; cin> nowWord; if (ispunct (nowWord [nowWord. size ()-1]) nowWord = nowWord. substr (0, nowWord. size ()-1); result = beforeWord = nowWord; int count = 1, maxCount = 1; while (cin> nowWord) {if (Ispunct (nowWord [nowWord. size ()-1]) nowWord = nowWord. substr (0, nowWord. size ()-1); if (beforeWord = nowWord) {count ++;} else {beforeWord = nowWord; count = 1;} if (count> maxCount) {maxCount = count; result = nowWord;} if (maxCount = 1) cout <"There is no word repeating. "<endl; else cout <" The word of "<result <" repeats: "<maxCount <" times. "<endl; return 0 ;}</pre> <Pre> </pre> <p> </p> <pre> </pre> is here to share with you your reference or provide good ideas for reference and improvement .. Thank you ~~ <P> </p> <span style = "font-family:, Arial, Helvetica, san-serif; font-size: 14px; line-height: 22.390625px "> </span> </p> <span style =" font-family:, Arial, Helvetica, san-serif; font-size: 14px; line-height: 22.390625px "> </span> </p> <pre> </pre>