uva 10132 - File Fragmentation

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
一个相同的串给出一些一分为二的不同分法,找出原来那个串,先入为主的做法就是算出原串长度然后枚举所有可能组合再判断,枚举的策略则是最长的+最短的;
后来想到可以直接更具最长的两个子串直接求出原串;
01110
   10111
10111
   01110
最长的两个串一定包含在原串他们组合的方式只有两种,重叠部分相同时的满足条件;
这样做的话有些特例如
0000
0000
1111
1111
所以我们找一个最长的子串,再找一个第二长的尽量不和第一个重复,之后只要判断这两个串的两种组合,哪种重叠部分相同即可;
#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;  //先*2再除否则L不能整除n,wrong了一次
  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;
}

你可能感兴趣的:(File,Integer,less,input,each,output)