Thursday, March 17, 2011


Problem Links:



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.


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.


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



Sample Output




Source Code: 

//Thu Mar 17 15:57:46 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

void initialize(vector<string> &v)
    string str;
    while (getline(cin, str, '\n'))
        if (str == "") break;
        //cout << str << endl;

bool cmp(const string &A, const string &B)
    int szA = A.size();
    int szB = B.size();
    if (szA < szB) return true;
    if (szA == szB && < 0)
        return true;
    return false;

bool check(vector<string> v, string str)
    for (int i = 0; i < v.size(); i++)
        int found1 = str.find(v[i]);
        int found2 = str.rfind(v[i]);
        if (found1 != 0 && found2 + v[i].size() != str.size()) return false;
    return true;

string solve(vector<string> v)
    int szmin = v[0].size();
    int szmax = v[v.size() - 1].size();
    for (int i = 0; v[i].size() == szmin; i++)
        for (int j = v.size() - 1; v[j].size() == szmax; j--)
            string str = v[i] + v[j];
            if (check(v, str))
                return str;
            str = v[j] + v[i];
            if (check(v, str))
                return str;
    return "";

int main(int argc, char* argv[])
    //freopen("", "r", stdin);
    //freopen("output.out", "w", stdout);
    int T;
    cin >> T;
    for (int ncase = 1; ncase <= T; ncase++)
        if (ncase > 1) cout << endl;
        vector<string> frags;
        std::sort(frags.begin(), frags.end(), cmp);
        cout << solve(frags) << endl;
    return 0;

No comments :