Hoj 1307 Choose Your Words Carefully, Hoj 1989 The Most Frequ

Source: Internet
Author: User

Although this question is a water question, I still prefer it. It is similar to the lexical analysis of compilation principles. The question is to count the words that appear frequently in an article. Use map <string, int> for statistics. In addition, the question should be arranged in Lexicographic Order, but it seems that Map already has this function, so it will not be written. [Cpp] # include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <math. h >#include <algorithm> # include <stack> # include <queue> # include <map> using namespace std; int main () {# ifndef ONLINE_JUDGE freopen ("in.txt ", "r", stdin); # endif char word [122]; string a; map <string, int> m; int max = 1; while (scanf ("% s ", word )! = EOF) {. clear (); int flag = 0; for (int I = 0; I <strlen (word); I ++) {if (isalpha (word [I]) | (word [I]> = '0' & word [I] <= '9') {flag = 1; if (word [I]> = 'A' & word [I] <= 'Z') {word [I] + = 32;}. push_back (word [I]);} else if (flag = 1) {if (m. find (a) = m. end () {m. insert (make_pair (a, 1);} else {m [a] ++; if (m [a]> max) {max = m [a];} a. clear (); flag = 0 ;}} if (flag = 1) {if (m. find (a) = m. end ()){ M. insert (make_pair (a, 1);} else {m [a] ++; if (m [a]> max) {max = m [a] ;}}} printf ("% d occurrences \ n", max); for (map <string, int >:: iterator I = m. begin (); I! = M. end (); I ++) {if (I-> second = max) {printf ("% s \ n", I-> first. c_str () ;}} return 0;} similar to the previous question, I used the question code A directly. [Cpp] # include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <math. h >#include <algorithm> # include <stack> # include <queue> # include <map> using namespace std; int main () {# ifndef ONLINE_JUDGE freopen ("in.txt ", "r", stdin); # endif char word [122]; string a; map <string, int> m; int max = 1; while (scanf ("% s ", word )! = EOF & strcmp (word ,"#")! = 0) {. clear (); int flag = 0; for (int I = 0; I <strlen (word); I ++) {if (isalpha (word [I]) | (word [I]> = '0' & word [I] <= '9') {flag = 1; if (word [I]> = 'A' & word [I] <= 'Z') {word [I] + = 32;}. push_back (word [I]);} else if (flag = 1) {if (m. find (a) = m. end () {m. insert (make_pair (a, 1);} else {m [a] ++; if (m [a]> max) {max = m [a];} a. clear (); flag = 0 ;}} if (flag = 1) {if (m. find (a) = m. end () {m. Insert (make_pair (a, 1);} else {m [a] ++; if (m [a]> max) {max = m [a] ;}}}www.2cto.com // printf ("% d occurrences \ n", max); for (map <string, int> :: iterator I = m. begin (); I! = M. end (); I ++) {if (I-> second = max) {printf ("% s \ n", I-> first. c_str (); break;} return 0 ;}

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.