Question 2: file fragmentationthe Problem
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 you to ask for help putting them back together again.
Fortunately, all of the files on the tray were identical, all of them broke into exactly two fragments, and all of the file fragments were found. unfortunately, the files didn't all break in the same place, and the fragments were completely mixed up by their
Fall to the floor.
You 've translated the original binary fragments into strings of ASCII 1's and 0's, and you're planning to write a program to determine the bit pattern the files contained.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. this line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Input will 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 will be separated by a blank line.
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 shoshould be possible to concatenate these fragments together in pairs to make n copies of the output string. if there is no
Unique Solution, any of the possible solutions may be output.
Your friend is certain that there were no more than 144 files on the tray, and that the files were all less than 256 bytes in size.
Sample Input
1011011101110111011110111
Sample output
01110111 if a single string is used to identify the original string, the first method is to calculate the length of the original string, enumerate all possible combinations, and then judge, the enumeration policy is the longest + shortest;
Later I thought that I could directly obtain the original string from the two longest substrings;
01110
10111
10111
01110
The longest two strings must be contained in the original strings. They are combined in only two ways, and the overlapping parts meet the same conditions;
For example
0000
0000
1111
1111
So we need to find the longest substring and try to find another substring that is the second length and try not to repeat it with the first one. Then, we only need to determine which of the two combinations of the two strings has the same overlap;
# Include <string. h> # include <stdio. h> char s [300] [300], F1, F2, ans1 [300], ans2 [300]; int main () {int I, j, T, n = 0, L = 0, L, Len [300], temp; scanf ("% d", & T); getchar (); fgets (s [0], 256, stdin); While (t --) {If (t = 0) while (scanf ("% s", & S [++ N])! = EOF) {L = strlen (s [N]);} else {While (fgets (s [++ N], 256, stdin )) {If (s [N] [0] = '\ n') break; L = strlen (s [N]); s [N] L-1] = '\ 0' ;}} for (I = 1; I <n; I ++) {Len [I] = strlen (s [I]); L + = Len [I];} -- N; L = 2 * l/N; // remove 2 first, otherwise l cannot divide N, wrong once for (I = 1; I <n; I ++) for (j = I + 1; j <= N; j ++) {If (LEN [I] <Len [J]) {Len [0] = Len [I]; len [I] = Len [J]; Len [J] = Len [0]; strcpy (s [0], s [I]); strcpy (s [I], s [J]); strcpy (s [J], s [0]) ;}} ans1 [l] = '\ 0 '; F1 = 1; F2 = 1; temp = 2; while (temp + 1 <= N & (strcmp (s [1], s [temp]) = 0 )) ++ temp; strcpy (s [2], s [temp]); for (I = 0; I <Len [1]; I ++) ans1 [I] = s [1] [I]; for (I = 0; I <Len [2]; I ++) {If (ans1 [L-len [2] + I]! = S [2] [I] & (L-len [2] + I <Len [1]) F1 = 0; ans1 [L-len [2] + I] = s [2] [I];} for (I = 0; I <Len [2]; I ++) ans2 [I] = s [2] [I]; for (I = 0; I <Len [1]; I ++) {If (ans2 [L-len [1] + I]! = S [1] [I] & (L-len [1] + I <Len [2]) F2 = 0; ans2 [L-len [1] + I] = s [1] [I];} If (F1) puts (ans1); else puts (ans2); n = 0; L = 0; If (t) printf ("\ n");} return 0 ;}