[暴力]uva10132 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

題意:原本有很多個一樣的長串,每個長串都分成了兩個短串但是分割的位置不一定相同即兩個短串不一定是平分的(就是輸入中的那些串)。你要把這偶數個短串拼回那個長串(即2N個短串拼回N個相同的長串),如果有多種可能,任意一種即可,然後輸出這個長串

思路:輸入有點噁心,首先是case數,然後一個空行,然後再輸入信息,然後又以一個空行結束該組數據,如果是最後一組數據了,不以空行結束,而是直接以EOF結束。輸出,case之間有一個空行,最後一個case後面不要加空行

每組case的信息,就是N行的串,串中只有01,串的個數一定是偶數。

數據不大,144,直接暴力。首先按短串的長度升序排序,然後我們知道,長串的長度 = 最短的短串長度 + 最長的短串的長度。所以我們枚舉最短的串和最長的串來組成一個長串,以這個長串也檢驗所有的組合,能成功的話立馬輸出。

代碼寫的超級亂。。ORZ。。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>

using namespace std;

vector<string> vec;
int tag[500];

bool cmp(string str1,string str2)
{
    return str1.size()<str2.size();
}

bool judge(string s)
{
    memset(tag,0,sizeof(tag));
    for(int i=0;i<vec.size();i++)
    {
        if(tag[i]==0)
        {
            int flag=0;
            for(int j=0;j<vec.size();j++)
            {
                if((vec[i]+vec[j]==s||vec[j]+vec[i]==s)&&tag[j]==0&&j!=i)
                {
                    flag=1;
                    tag[i]=tag[j]=1;
                    break;
                }
            }
            if(flag==0) return false;
        }
    }
    for(int i=0;i<vec.size();i++)
        if(tag[i]==0) return false;
    return true;
}

int main()
{
    int num;
    cin>>num;
    string str;
    getline(cin,str);
    getline(cin,str);
    while(num--)
    {
        vec.clear();
        if(num>0)
        {
            while(getline(cin,str)&&str!="")
            {
                vec.push_back(str);
            }
        }
        else if(num==0)
        {
            while(getline(cin,str))
            {
                vec.push_back(str);
            }
        }
        sort(vec.begin(),vec.end(),cmp);
        int len=vec.size();
        int minlen=vec[0].size(),maxlen=vec[len-1].size();
        int minpos,maxpos,i,j,k;
        for(i=0;i<len;i++)
        {
            if(vec[i].size()>minlen)
            {
                minpos=i;
                break;
            }
        }
        for(i=len-1;i>=0;i--)
        {
            if(vec[i].size()<maxlen)
            {
                maxpos=i;
                break;
            }
        }
        if(maxlen==minlen)
        {
            minpos=0;
            maxpos=vec.size()-1;
        }
        int flag=0;
        for(i=0;i<=minpos;i++)
        {
            for(j=len-1;j>=maxpos;j--)
            {
                if(i!=j)
                {
                    string str1=vec[i]+vec[j];
                    string str2=vec[j]+vec[i];
                    if(judge(str1))
                    {
                        cout<<str1<<endl;
                        flag=1;
                        break;
                    }
                    if(judge(str2))
                    {
                        cout<<str2<<endl;
                        flag=1;
                        break;
                    }
                }
            }
            if(flag) break;
        }
        if(num) cout<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章