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