I haven't handed in questions for a long time, and I found that the recent bad status is not good, and the AC rate is disgusting ~
This questionCodeA little long, but the actual idea is very simple. qsort takes a look at the order and then matches the longest string with the shortest string, because a string can be divided into two different longest strings at most, therefore, the longest match with the shortest substring is a maximum of eight cases. One of them is the answer.
The Code is as follows:
# Include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> using namespace STD; int CMP (const void * a, const void * B) {int size = strlen (char *) A)-strlen (char *) B); If (! Size) return strcmp (char *) A, (char *) B); Return size;} int main () {# ifdef st freopen ("sample.txt", "r ", stdin); # endif char a [150] [260], B [260], C [260]; int num; scanf ("% d", & num ); getchar (); While (Num --) {int Ct = 0; memset (A, 0, sizeof (a); While (1) {gets (A [cT]); If (! Strlen (A [cT]) break; ++ CT;} qsort (A, CT, sizeof (A [0]), CMP); int flag = 0; if (strlen (A [0]) = strlen (A [1]) {sprintf (B, "% S % s", a [0], A [CT-1]); sprintf (C, "% S % s", a [CT-2], a [1]); If (! Strcmp (B, c) // if the two strings are the same, the request is complete string flag = 1; if (! Flag) {sprintf (B, "% S % s", a [1], a [CT-1]); sprintf (C, "% S % s ", A [CT-2], a [0]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [0]); sprintf (C, "% S % s ", A [1], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [1]); sprintf (C, "% S % s ", A [0], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [0], a [CT-1]); sprintf (C, "% S % s ", A [1], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [1], a [CT-1]); sprintf (C, "% S % s ", A [0], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [0]); sprintf (C, "% S % s ", A [CT-2], a [1]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [1]); sprintf (C, "% S % s ", A [CT-2], a [0]); If (! Strcmp (B, c) Flag = 1 ;}} else sprintf (B, "% S % s", a [CT-1], a [0]); printf ("% s \ n", B); If (Num) puts ("");} return 0 ;}