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
   这题目用暴力枚举就可以,但还是错了好多次,最后检查出错在输入的地方了。 仔细读一下输入要求 相邻两个数据之间用空行隔开,最后那个数据后面是没有空行的。 而我再输入的时候写成了死循环,结果在输入最后一组数据的时候是不会处理数据的,应该写成处理到文件结束,悲催,检查了好久才检查出来
#include <stdio.h>
#include <string.h>
#include <math.h>
char s1[1000][1000];
char s2[1000],s3[1000];
int main()
{
    int i,j,n,m,s,t,l1,l2,z,u,v,k,l;
    scanf("%d%*c",&t);
    gets(s2);
    while(t--)
    {
        n=0;
        while(gets(s1[n]))
        {
            l=strlen(s1[n]);
            if(l==0)
            {
                break;
            }
            n++;
        }
        for(i=0;i<=n-1;i++)
        {
            for(j=0; j<= n-1;j++)
            {
                if(i==j)
                {
                    continue;
                }
               strcpy(s2,s1[i]);
               strcat(s2,s1[j]);
               l1= strlen(s2);
               for(z=0,k=0; z<= n-1 ;z++)
               {
                   if(z==i||z==j)
                   {
                       continue;
                   }
                   l2=strlen(s1[z]);
                   if(s2[0]==s1[z][0])
                   {
                       for(u=0; u<=l1-1&&u<=l2-1; u++)
                       {
                           if(s1[z][u]!=s2[u])
                           {
                               break;
                           }
                       }
                       if(u==l2)
                       {
                           for(v=u;v<=l1-1;v++)
                           {
                               s3[v-u]=s2[v];
                           }
                           s3[v-u]='\0';
                           for(u=0; u<=n-1; u++)
                           {
                               if(u==i||u==j||u==z)
                               {
                                   continue;
                               }
                               if(strcmp(s3,s1[u])==0)
                               {
                                   break;
                               }
                           }
                           if(u!=n)
                           {
                              k=1;
                           }
                       }
                   }
                   if(k==1)
                   {
                       k=0;
                       continue;
                   }
                   if(s2[l1-1]==s1[z][l2-1])
                   {
                      for(u=l2-1,v=l1-1;u>=0,v>=0; u--,v--)
                      {
                          if(s1[z][u]!=s2[v])
                          {
                              break;
                          }
                      }
                      if(u==-1)
                      {
                          for(u=0; u<= v;u++)
                          {
                              s3[u]=s2[u];
                          }
                          s3[u]='\0';
                          for(u=0;u<=n-1;u++)
                          {
                              if(u==i||u==j||u==z)
                              {
                                  continue;
                              }
                              if(strcmp(s3,s1[u])==0)
                              {
                                  break;
                              }
                          }
                          if(u!=n)
                          {
                              k=1;
                          }
                      }
                   }
                   if(k==1)
                   {
                       k=0;
                   }else
                   {
                       break;
                   }
               }
               if(z==n)
               {
                   break;
               }
            }
            if(j!=n)
            {
                break;
            }
        }
        printf("%s\n",s2);
        if(t)
        {
            printf("\n");
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值