Uva--10132(技巧)

2014-07-23 21:05:19

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

思路:这题有点意思,由于不能在同个位置切两次。排完序后只有两种情况:(1)只有一个最短长度的串:只用枚举s(1)、s(n)和s(2)、s(n-1)的四种组合情况s(1)+s(n),s(n)+s(1)和
s(2)+s(n-1),s(n-1)+s(2),若能从前者和后者中各找一个使两者相等就得到答案了。(2)有两个最短长度的串:最短长度的串的个数最多只有两个(因为形成相等长度最短串只能在对称位置切)
s(1)可能和s(n-1)或s(n)组合,枚举两种情况即可。(一种情况可以利用(1)来判断,如果(1)不行再考虑(2))。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 struct Str{
 9     char s[300];
10 }f[300];
11 bool cmp(Str a,Str b){
12     return strlen(a.s) < strlen(b.s);
13 }
14 int main(){
15     char s1[300],s2[300],s3[300],s4[300];
16     int Case,cnt;
17     scanf("%d",&Case);
18     getchar();
19     getchar();
20     while(Case--){
21         cnt = 0;
22         while(gets(f[cnt].s)){
23             if(f[cnt].s[0] == '\0')     break;
24             ++cnt;
25         }
26         sort(f,f + cnt,cmp);
27         memcpy(s1,f[0].s,sizeof(f[0].s));
28         strcat(s1,f[cnt - 1].s);
29         memcpy(s2,f[cnt - 1].s,sizeof(f[cnt - 1].s));
30         strcat(s2,f[0].s);
31 
32         memcpy(s3,f[1].s,sizeof(f[1].s));
33         strcat(s3,f[cnt - 2].s);
34         memcpy(s4,f[cnt - 2].s,sizeof(f[cnt - 2].s));
35         strcat(s4,f[1].s);
36 
37         if(strcmp(s1,s3) == 0 || strcmp(s1,s4) == 0)    printf("%s\n",s1);
38         else if(strcmp(s2,s3) == 0 || strcmp(s2,s4) == 0)   printf("%s\n",s2);
39         else{
40             memcpy(s1,f[0].s,sizeof(f[0].s));
41             strcat(s1,f[cnt - 2].s);
42             memcpy(s2,f[cnt - 2].s,sizeof(f[cnt - 2].s));
43             strcat(s2,f[0].s);
44 
45             memcpy(s3,f[1].s,sizeof(f[1].s));
46             strcat(s3,f[cnt - 1].s);
47             memcpy(s4,f[cnt - 1].s,sizeof(f[cnt - 1].s));
48             strcat(s4,f[1].s);
49 
50             if(strcmp(s1,s3) == 0 || strcmp(s1,s4) == 0)    printf("%s\n",s1);
51             else    printf("%s\n",s2);
52         }
53         if(Case) printf("\n");
54     }
55     return 0;
56 }

 





转载于:https://www.cnblogs.com/naturepengchen/articles/3864035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值