Description
Question 2: File Fragmentation
The 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 should 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
1 011 0111 01110 111 0111 10111
Sample Output
01110111
题意:有n份相同的文件内容全部有0和1组成,由于失误吧文件全部损坏了,每份文件都变成了残缺的两份(每一份都变成了两份),现在要你输出该文件的内容。
分析:因为每份文件都变成了两份,那么可以认为 最小的碎片长度+最大的碎片长度 = 该文件的长度。最小的碎片+最大的碎片 的其中一个组合一定是正确的文件顺序。将所有的组合存起来然后和第二小的碎片+ 第二大的碎片的组合进行比较,相同的哪一个组合就是符合要求的一个值。
(AC了之后想想才发现这貌似是最麻烦的一种方法。)
代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> char a[201][601]; int n; int cmp(const void* a, const void* b) { return strlen((char*)a) > strlen((char*)b); } int main() { int T; char ss[100]; scanf("%d\n",&T); while(T--) { n = 0; while (gets(a[n]) && a[n][0]) ++n; qsort(a,n,sizeof(a[0]),cmp); int flag = 0; char str[5][501]; char str1[5][501]; memset(str,0,sizeof(str)); memset(str1,0,sizeof(str1)); strcpy(str[0],a[0]); strcpy(str[1],a[n-1]); strcat(str[0],a[n-1]); strcat(str[1],a[0]); int m = 2; for(int i=1; i<n/2; i++) { if(strcmp(a[i],a[0]) != 0 && strlen(a[0]) == strlen(a[i])) { strcpy(str[m],a[0]); strcat(str[m],a[n-i-1]); m++; strcpy(str[m],a[n-i-1]); strcat(str[m],a[0]); m++; strcpy(str1[0],a[i]); strcpy(str1[1],a[n-1-i]); strcat(str1[0],a[n-1-i]); strcat(str1[1],a[i]); int mm = 2; strcpy(str1[mm],a[i]); strcat(str1[mm],a[n-1]); mm++; strcpy(str1[mm],a[n-1]); strcat(str1[mm],a[i]); mm++; for(int pi=0; pi<m; pi++) { for(int pj=0; pj<mm; pj++) { if(strcmp(str1[pj],str[pi]) == 0) { printf("%s\n",str[pi]); flag = 1; break; } } if(flag == 1) { break; } } break; } else if(strlen(a[i])!=strlen(a[0])) { strcpy(str1[0],a[i]); strcpy(str1[1],a[n-1-i]); strcat(str1[0],a[n-1-i]); strcat(str1[1],a[i]); int mm = 2; for(int j=i+1; j<n/2; j++) { if(strlen(a[j]) == strlen(a[i]) && strcmp(a[j],a[i])!=0) { strcpy(str1[mm],a[i]); strcpy(str1[mm],a[n-1-j]); mm++; strcat(str1[mm],a[n-1-j]); strcat(str1[mm],a[i]); break; } } for(int pi=0;pi<m;pi++) { for(int pj=0;pj<mm;pj++) { if(strcmp(str[pi],str1[pj]) == 0) { printf("%s\n",str[pi]); flag = 1; break; } } if(flag == 1) { break; } } break; } } if(flag == 0) { printf("%s\n",str[0]); } if(T) { printf("\n"); } } return 0; }