UVa 10132 - File Fragmentation

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1073


类型: 贪心+回溯


原题:

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



题目大意:

你朋友端着一盘子,上面有N份相同的文件,但是不小心盘子掉到地上了,每一个文件都正好摔断成2份。现在用0,1组成的字符串代表原来的文件, 然后输入所有的“碎片”,根据这些碎片求出原文件。


分析与总结:

第一次看到这题时想到的是用回溯来做,但是直接回溯的话肯定是会超时的,必须再加上贪心的思想,和一些减枝。

首先要求出原字符串的长度,这个长度等于最短的字符串长度+最长的字符串长度。

然后,把所有字符串按照长度从小到大排序。

接着就是进行回溯搜索的过程了:

在进行回溯时,首先是枚举所有可能的原字符串,在对目前这个原字符串进行搜索匹配。

在匹配时,由于字符串已经根据长度从小到达排序了,假设总数为n, 那么用来组装的“碎片”其中一个一定是在0~n/2中,而另一个一定是在(n/2+1)~n中。根据这个结论,那么在枚举两个碎片时,第一层for循环搜索范围从1~(n/2),  第二层for循环为(n-1)~(n/2+1).  在第二层循环中, 从n-1倒过来枚举,一旦两个长度小于原长度,那么就可以直接退出。

这个方法最终运行时间为0.008s.


代价:

/*
 * UVa: 10132 - File Fragmentation
 * Time: 0.008s
 * Author: D_Double
 *
 */
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#define MAXN 300
using namespace std;

map<string, bool>mp;
char str[MAXN][260], ans[260];
int nFile, len;
bool vis[MAXN], flag;

int cmp(const void *a, const void *b){
    int l1=strlen((char*)a), l2=strlen((char*)b);
    if(l1!=l2) return l1-l2;
    return strcmp((char*)a,(char*)b);
}

inline void input(){
    nFile=0;
    int minLen=10000, maxLen=-10000;
    while(gets(str[nFile])){
        if(!str[nFile][0])break;
        int l=strlen(str[nFile]);
        if(l<minLen)minLen=l;
        if(l>maxLen)maxLen=l;
        ++nFile;
    }
    len=maxLen+minLen; //求出原串的长度,等于最小碎片与最长碎片长度之和
}

void search(int cnt, char *file){
    if(flag)return;
    if(cnt==nFile/2){
        strcpy(ans, file);
        flag=true;
        return;
    }

    for(int i=0; i<nFile/2; ++i)if(!vis[i]){
        vis[i] = true;
        if(cnt==0){ // 当cnt等于0时,枚举所有可能的“原字符串”进行搜索
            for(int j=nFile-1; j>=nFile/2; --j)if(!vis[j]){ 
                int l=strlen(str[i])+strlen(str[j]);
                if(l<len) return; // 减枝,当长度小于原长度,退出,因为后面的长度会更小
                if(l>len) continue;

                char temp1[300];
                strcpy(temp1, str[i]);
                strcat(temp1, str[j]);
                if(!mp[temp1]){   // 用map来检查这个字符串是否搜索过,这个非常重要!!
                    mp[temp1]=true;
                    vis[j] = true;
                    search(cnt+1, temp1);
                }

                char temp2[300];
                strcpy(temp2, str[j]);
                strcat(temp2, str[i]);
                if(strcmp(temp1, temp2)==0)continue;
                if(!mp[temp2]){ // 用map来检查这个字符串是否搜索过,这个非常重要!!
                    mp[temp2]=true;
                    vis[j]=true;
                    search(cnt+1, temp2);
                }
                vis[j] = false;
            }
        }
        else{
            for(int j=nFile-1; j>=nFile/2; --j)if(!vis[j]){
                int l=strlen(str[i])+strlen(str[j]);
                if(l<len) return;
                if(l>len) continue;
                
                char temp1[300];
                strcpy(temp1, str[i]);
                strcat(temp1, str[j]);
                if(strcmp(temp1, file)==0){
                    vis[j] = true;
                    search(cnt+1, temp1);
                }

                char temp2[300];
                strcpy(temp2, str[j]);
                strcat(temp2, str[i]);
                if(strcmp(temp1, temp2)!=0){
                    vis[j] = true;
                    search(cnt+1, temp2);
                }
                vis[j] = false;
            } 
        }
        vis[i] = false;
    }
}

int main(){
    int T;
    scanf("%d%*c",&T);
    gets(str[0]); // 消除空行
    while(T--){
        input();
        qsort(str, nFile, sizeof(str[0]), cmp);
        memset(vis, 0, sizeof(vis));
        mp.clear();
        char t[10]="abc"; // 随便传一个参数进去
        flag=false;
        if(nFile>2)search(0, t);
        else{strcpy(ans, str[0]); strcat(ans, str[1]);}
        puts(ans);
        if(T)printf("\n");
    }
}


——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)


你可能感兴趣的:(UVa 10132 - File Fragmentation)