Simulation of ZOJ-3700 ever dream

Source: Internet
Author: User
Tags strtok

Meaning: given some lyrics, the lyrics are given by a row and a row. All words with more than 2 times are output. words with the same number of times are grouped into one group, with the maximum output length in each group, otherwise, the last and second words in the Lexicographic Order are output.

Solution: the definition of the word is actually fuzzy. The correct method is to convert all non-letters into spaces before processing. Use Map to count the number of words, and then use an array to sort out the order.

CodeAs follows:

# Include <cstdlib> # Include <Cstdio> # Include <Cstring> # Include <Algorithm> # Include <Iostream> # Include <Cctype> # Include < String > # Include <Map>Using   Namespace  STD;  Char STR [ 105  ]; Map < String , Int > MP; Map < String , Int > : Iterator it;  Struct  Word {  String W;  Int  CNT;}; word e [  1000  ];  Int  Idx;  Bool   Operator <( Const Word &, Const Word & B ){  If (A. CNT! = B. CNT ){ //  Place High Frequency before         Return A. CNT> B. CNT ;}  Else   If (A. W. Size ()! = B. W. Size ()){ //  Put the front with a long length          Return A. W. Size ()> B. W. Size ();}  Else  {  Return A. W> B. W; //  When the Lexicographic Order is large, the last and last words must be output, so that the second positive number is output.  }} Void  Deal (){  Char Ss [ 105 ], * P;  Int Len = Strlen (STR );  For ( Int I = 0 ; I <Len; ++ I ){  If (! Isalpha (STR [I]) {STR [I] = '  '  ;} P = Strtok (STR, "   "  );  While  (P ){  Bool Flag = True  ; Strcpy (SS, P); Len = Strlen (SS );  For ( Int I = 0 ; I <Len; ++I ){  If (! Isalpha (ss [I]) {flag = False ; //  It indicates that this is not a letter                  Break  ;}  Else   If  (Isupper (ss [I]) {ss [I] + = 32 ; //  Convert to lowercase letters }}  If  (FLAG ){ ++ MP [ss];} p = Strtok (null, "   "  );}}  /*  1233a a AB B BC c c3abab abababab ababdd dd32132liuxu nihao liuxuliuxu nihao  */  Int  Main (){  Int  T, N; For (Scanf ( "  % D  " , & T); t ;-- T) {MP. Clear (); idx = 0  ; Scanf (  "  % D  " ,& N), getchar ();  For ( Int I = 0 ; I <n; ++I) {gets (STR); Deal ();}  For (It = mp. Begin (); it! = Mp. End (); ++ It ){  If (It-> second> = 2  ) {E [idx]. W = It-> First; E [idx ++]. CNT = it-> Second ;}} sort (E, E + Idx); e [idx]. CNT = 0 ; // Prevent the last word from being misjudged          Int Last = 0 , First = 1  ;  For ( Int I = 0 ; I <idx; ++ I ){  If (E [I]. CNT! = Last ){  Int K = I;  If (E [I]. CNT = E [I + 1 ]. CNT & E [I]. W. Size () = E [I + 1 ]. W. Size () ++ K;  If  (First) {printf (  "  % S  "  , E [K]. W. c_str (); first = 0  ;}  Else  {Printf ( "  % S  "  , E [K]. W. c_str ();} Last = E [I]. CNT;} puts (  ""  );}  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.