UVA 10132 - File Fragmentation

来源:互联网 发布:ssd固态硬盘优化软件 编辑:程序博客网 时间:2024/06/05 17:08

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

1011011101110111011110111

Sample Output

01110111

================================

每次输入完毕后都会再输入一个空行……每组输出之间也要有空行……

先排序,总长度就是a[0]和a[n-1]的长度和。

长度相等的最多有两个(不会有两个文件在同一个地方断掉),所以只要出现有长度相等的ab,和与它们对应的长度相等的两个cd一定可以组成两份相同的原文件。只有ac ad bc bd四种方式,枚举即可。

如果都只出现一次,那么a[0]和a[n-1]、a[1]和a[n-2]一定可以组成两份相同的原文件,同样枚举即可。


#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;string str[333],s1,s2;char s[333];int cmp(string a,string b){    if(a.length()==b.length())        return a<b;    else return a.length()<b.length();}int main(){    int t;    scanf("%d",&t);    getchar();    getchar();    while(t--)    {        int n=0,p=-1;        while(gets(s))        {            if(s[0]!='\0')                str[n++]=s;            else break;        }        sort(str,str+n,cmp);        //for(int i=0;i<n;i++) cout<<"---"<<str[i]<<endl;        int sumlen=str[0].length()+str[n-1].length();        for(int i=0;i<n-1;i++)        {            if(str[i].length()==str[i+1].length())            p=i;            break;        }        if(p!=-1)        {            if(str[p]+str[n-1-p]==str[p+1]+str[n-2-p]) cout<<str[p]+str[n-1-p]<<endl;            else if(str[p]+str[n-1-p]==str[n-2-p]+str[p+1]) cout<<str[p]+str[n-1-p]<<endl;            else if(str[n-1-p]+str[p]==str[p+1]+str[n-2-p]) cout<<str[n-1-p]+str[p]<<endl;            else if(str[n-1-p]+str[p]==str[n-2-p]+str[p+1]) cout<<str[n-1-p]+str[p]<<endl;        }        else        {            if(str[0]+str[n-1]==str[1]+str[n-2]) cout<<str[0]+str[n-1]<<endl;            else if(str[0]+str[n-1]==str[n-2]+str[1]) cout<<str[0]+str[n-1]<<endl;            else if(str[n-1]+str[0]==str[1]+str[n-2]) cout<<str[n-1]+str[0]<<endl;            else if(str[n-1]+str[0]==str[n-2]+str[1]) cout<<str[n-1]+str[0]<<endl;        }        if(t) cout<<endl;    }    return 0;}


原创粉丝点击