連結:
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
1011011101110111011110111
Sample Output
01110111
題目大意:
你朋友端著一盤子,上面有N份相同的檔案,但是不小心盤子掉到地上了,每一個檔案都正好摔斷成2份。現在用0,1組成的字串代表原來的檔案, 然後輸入所有的“片段”,根據這些片段求出原檔案。
分析與總結:
更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
第一次看到這題時想到的是用回溯來做,但是直接回溯的話肯定是會逾時的,必須再加上貪心的思想,和一些減枝。
首先要求出原字串的長度,這個長度等於最短的字串長度+最長的字串長度。
然後,把所有字串按照長度從小到大排序。
接著就是進行回溯搜尋的過程了:
在進行回溯時,首先是枚舉所有可能的原字串,在對目前這個原字串進行搜尋匹配。
在匹配時,由於字串已經根據長度從小到達排序了,假設總數為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"); } }
作者:csdn部落格 shuangde800