The main idea: the input is a bit disgusting, first, the case number, then a blank line, and then enter the information, and then a blank line to end the group of data, if it is the last set of data, not to end with a blank line, but the direct end of EOF. Output, there is a blank line between case, the last case does not empty the information of each set of case, is the string of n rows, only 01 in the string, the number of strings must be even. There were a lot of the same long string, each long string is divided into two short strings but the location of the split is not necessarily the same two short strings are not necessarily equally divided (that is, those strings in the input). You want to put this odd number of short strings back to that long string (that is, 2N short string to spell back n the same long string), if there are many possibilities, any one can, and then output this long string of topics to ensure there are solutions to solve the problem: data is not big, 144, direct violence. First sort by the length of the short string, and then we know the length of the long string = the shortest short string length + the longest short string length. So we enumerate the shortest string and the longest string to form a long string, and this long string also examines all the combinations, which can be successfully output immediately.
#include <cstdio>#include <iostream>#include <string>#include <algorithm>using namespace STD;stringa[ -];intNBOOLcmpstringAstringb) {returnA.size () < B.size ();}BOOLJudgestringWord) {intvis[ -] = {0}; for(inti =0; I < n/2; i++) { for(intj = N-1; J >= N/2; j--)if(!vis[j] && (A[i] + a[j] = = Word | | A[J] + a[i] = = Word) {Vis[j] = vis[i] =1; Break; }if(!vis[i])return false; }cout<< word << Endl;return true;}voidSolve () { for(inti =0; A[i].size () = = a[0].size (); i++) for(intj = N-1; A[j].size () = = a[n-1].size (); j--)if(Judge (A[i] + a[j]) | | judge (A[J] + a[i]))return;}intMain () {intTscanf("%d\n", &t);Charstr[ -]; while(t--) {n =0; while(Gets (str)! = NULL && str[0]) a[n++] = str; Sort (A, a + N, CMP); Solve ();ifTputs(""); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-10132 File Fragmentation