A small c ++ programming question,

Source: Internet
Author: User

 

<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>

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.