UVaOJ10132 - File Fragmentation

10132 - File Fragmentation

Time limit: 3.000 seconds

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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int MAXN = 300;
char str[MAXN][260], ans[260];
int nFiles, len;
bool vis[MAXN], flag;
map<string, bool>mp;
int cmp(const void *a, const void *b) {
    int l1 = strlen((char *)a);
    int l2 = strlen((char *)b);
    if (l1 != l2) {
        return l1 - l2;
    } else {
        return strcmp((char *)a, (char *)b);
    }
}
void init_input() {
    nFiles = 0;
    int minLength = 1000, maxLength = -1000;
    while (gets(str[nFiles])) {
        if (! str[nFiles][0]) break;
        int l = strlen(str[nFiles]);
        if (l > maxLength) maxLength = l;
        if (l < minLength) minLength = l;
        nFiles ++;
    }
    len = minLength + maxLength;
}
void ssearch(int cnt, char *file) {
    if (flag) return;
    if (cnt == nFiles / 2) {
        strcpy(ans, file);
        flag = true;
        return;
    }
    for (int i = 0; i < nFiles / 2; i ++) {
        if (! vis[i]) {
            vis[i] = true;
            if (cnt == 0) {
                for (int j = nFiles - 1; j >= nFiles / 2; j --) {
                    if (! vis[j]) {
                        int l = strlen(str[i]) + strlen(str[j]);
                        if (l < len) return;
                        if (l > len) continue;
                        char temp1[MAXN];
                        strcpy(temp1, str[i]);
                        strcat(temp1, str[j]);
                        if (! mp[temp1]) {
                            mp[temp1] = true;
                            vis[j] = true;
                            ssearch(cnt + 1, temp1);
                        }
                        char temp2[MAXN];
                        strcpy(temp2, str[j]);
                        strcat(temp2, str[i]);
                        if (strcmp(temp1, temp2) == 0) continue;
                        if (! mp[temp2]) {
                            mp[temp2] = true;
                            vis[j] = true;
                            ssearch(cnt + 1, temp2);
                        }
                        vis[j] = false;
                    }
                }
            } else {
                for (int j = nFiles - 1; j >= nFiles / 2; j --) {
                    if (! vis[j]) {
                        int l = strlen(str[i]) + strlen(str[j]);
                        if (l < len) return;
                        if (l > len) continue;
                        char temp1[MAXN];
                        strcpy(temp1, str[i]);
                        strcat(temp1, str[j]);
                        if (strcmp(temp1, file) == 0) {
                            vis[j] = true;
                            ssearch(cnt + 1, temp1);
                        }
                        char temp2[MAXN];
                        strcpy(temp2, str[j]);
                        strcat(temp2, str[i]);
                        if (strcmp(temp2, temp1) != 0) {
                            vis[j] = true;
                            ssearch(cnt + 1, temp2);
                        }
                        vis[j] = false;
                    }
                }
            }
            vis[i] = false;
        }
    }
}
int main() {
    int T;
    scanf("%d%*c", &T);
    gets(str[0]);
    while (T --) {
        init_input();
        qsort(str, nFiles, sizeof(str[0]), cmp);
        memset(vis, false, sizeof(vis));
        mp.clear();
        char t[10]="abc";
        flag = false;
        if (nFiles > 2) {
            ssearch(0, t);
        } else {
            strcpy(ans, str[0]);
            strcat(ans, str[1]);
        }
        puts(ans);
        if (T) {
            printf("\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值