Uva 10132 — 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
题意:一个字符串分成两段,有多种分法,给出的由0 1组成的字符串,分别表示该字符串的一部分,即如样例中所显示,两个字符串能拼成原字符串,现在要求你由给出的字符串,求出元字符串;
分析:最长和最短的字符串一定能拼成原字符串,所以在根据字符串长度排序后,取第一个字串,与最后几个最长字符串进行组合,在与剩下的字符串进行比较。
代码如下:
#include <stdio.h>
#include <string>
#include<iostream>
#include<algorithm>
using namespace std;
int len;
struct node
{
    string  s;
    int l;
} ss[200];
bool cmp(node a,node b)
{
    return a.l<b.l;
}
int judge2(string a,int num)
{
    string c,d;
    for(int i=1; i<num; i++)
    {   c.assign(a,0,ss[i].l);//将a 从第0个字符开始长度为ss[i].l的字串赋給c;
        d.assign(a,a.size()-ss[i].l,ss[i].l);
        if(c!=ss[i].s&&d!=ss[i].s)
            return 0;  
    }
    return 1;
}
int main()
{
    int n,i,j;
    string s,ans;
    scanf("%d",&n);
    getchar();
    getchar();
    while(n--)
    {
        i=0;
        while(getline(cin, s))//getline(cin,s)感谢远航学长的帮助
        {   if(s == "")
                break;
            ss[i].s=s;ss[i].l=s.size();
            i++;  
        }
        len=i;
        sort(ss,ss+i,cmp);
        for(j=i-1; j>=0; j--)//找到最长字符串第一次出现的位置
            if(ss[j].l!=ss[j-1].l)
                break;

        for(int k=j; k<i; k++){
            ans=ss[0].s+ss[k].s;//ans代表组合得到的“原字符串”
            if(judge2(ans,k))
            {cout<<ans<<endl;
                break;  
            }
            ans=ss[k].s+ss[0].s;
            if(judge2(ans,k))
            {cout<<ans<<endl;
                break;    
            }
        }
        if(n)
            printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值