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<iostream>
#include<algorithm>
using namespace std;
string  s[1000];
string a,b;
int length,n;
char ss[1000];
bool cmp(string a,string b)
{
    return a.size()<b.size();
}
bool judge()
{
    for(int i=n-1; i>=0; --i)
    {
        if((s[i].size()+s[1].size())!=length) continue;
        b=s[1]+s[i];
        if(b==a)
            return true;
        b=s[i]+s[1];
        if(b==a)
            return true;
    }
    return false;
}
int main()
{
    int t;
    int MIN,MAX;
    scanf("%d\n",&t);
    while(t--)
    {
        n = 0;
        while (gets(ss)&&ss[0])
        {
            s[n]=ss;
            n++;
        }
        sort(s,s+n,cmp);
//        for(int i=0; i<n; i++)
//            cout<<s[i]<<endl;
        MIN=s[0].size();
        MAX=s[n-1].size();
        length=MIN+MAX;
        for(int i=n-1; i>=0&&s[i].size()==MAX; --i)
        {
            a=s[0]+s[i];
            if(judge())
                break;
            a=s[i]+s[0];
            if(judge())
                break;
        }
      cout<<a<<endl;
        if(t)
            cout<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值