10132-file fragmentation
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1073
Your friend, a biochemistry major, tripped while carrying a tray of computer files through the lab. All of the files fell to the ground and broke. Your friend picked up all the file fragments and called your to ask for help putting them back together again.
Fortunately, all of the files on the tray were identical, all of them broke to exactly two, and all of the fragments E fragments were found. Unfortunately, the files didn ' t all broke in the same place, and the fragments were completely-mixed up by their fall to T He floor.
You ' ve translated the original binary fragments into strings of ASCII 1 ' s and 0 ' s, and you ' re planning to write a To determine the bit pattern the contained.
Input
The input begins with a single positive integer in a line by itself indicating the number of cases following, each of them as described below. This are followed by a blank line, and there are also a blank line between two consecutive.
Input would consist of a sequence of ' ' File fragments ', one per line, terminated by the end-of-file marker. Each fragment consists of a string of ASCII 1 ' s and 0 ' s.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases is separated by a blank line.
The Output is a single line of ASCII 1 ' s and 0 's giving the bit pattern of the original files. If There are 2N fragments in the input, it should is possible to concatenate this fragments together in pairs to make N C Opies of the output string. If there is no unique solution, any of the possible solutions may be output.
Your Friend is certain this there were no more than 144 files on the tray, and which the files were all less than 256 bytes in size.
Sample Input
1
011
0111
01110
111
0111
10111
Sample Output
01110111
Learn English =v=~
1. All of the files in the tray were identical, all of them broke into exactly two.
All the files in the file box are exactly the same, and all the files are broken into two parts.
2. If there is no unique solution, any of the possible solutions could be output.
If there is no unique solution, any possible answer can be output. (but the title is not special Judge, so there is only one file spelling)
Idea: the shortest fragment length + the longest fragment length = the length of the original file, so enumerate the sorted s[0] and the longest fragments into the file, to compare the s[1] and other fragments of the file to see if the same.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
/*0.015s*/#include <cstdio> #include <cstring> #include <cstdlib> char s[160][260], a[260
], b[260];
int n, Len;
int CMP (const void* A, const void* b) {return strlen ((char*) a) < Strlen ((char*) b); BOOL Judge () {for (int i = n-1 i >= 0;-i) {if (strlen (s[1)) + strlen (S[i])!= l
EN) continue;
strcpy (b, s[1]);
strcat (b, s[i]);
if (strcmp (A, b) = = 0) return true;
If the current spelling of the file A is wrong (not the original file), then we will not be able to spell another same error file strcpy (b, s[i]);
strcat (b, s[1]);
if (strcmp (A, b) = = 0) return true;
return false;
int main () {int T, I, Min, Max;
scanf ("%d\n", &t);
while (t--) {n = 0;
while (gets (S[n]) && s[n][0]) ++n;
Qsort (s, N, sizeof (S[0]), CMP);
min = strlen (s[0]);
max = strlen (s[n-1]);
len = min + max; for (i =n-1; I >= 0 && (int) strlen (s[i]) = = max;
------///find those longest fragments to match s[0] {strcpy (A, s[0]);
Strcat (A, s[i]);
if (judge ()) break;
strcpy (A, s[i]);
Strcat (A, s[0]);
if (judge ()) break;
} puts (a);
if (T) Putchar (10);
return 0; }