File Fragmentation+uva+贪心
2014-07-15 19:50
309 查看
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 askfor 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 alsoa 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<cstdio> #include<algorithm> #include<string> #include<cstring> using namespace std; bool cmp(string a,string b){ return a.length()>b.length(); } string s[500];///用于存储文件碎片 bool vis[500];///用于记录用过的碎片 char str[500];///用于gets的输入 int h; bool search(string mp){ for(int i=0;i<=(h-1)>>1;i++){ vis[i]=true; bool flag=true; for(int j=h-1;j>i;j--){ if(!vis[j]&&((s[i]+s[j])==mp||(s[j]+s[i])==mp)){ vis[j]=true; flag=false; break; } } if(flag) return false; } return true; }///查找函数 int main(){ int t; cin>>t; getchar(); getchar(); while(t--){ h=0; while(1){ if(!gets(str)) break; if(str[0]=='\0') break; s[h++]=str; } sort(s,s+h,cmp); bool flag=true; for(int i=h-1;i>=0&&s[i].length()==s[h-1].length();i--){ memset(vis,false,sizeof(vis)); string mp=s[0]+s[i]; if(search(mp)){ cout<<mp<<endl; flag=false; break; } memset(vis,false,sizeof(vis)); mp=s[i]+s[0]; if(search(mp)){ cout<<mp<<endl; flag=false; break; } } if(flag){ cout<<s[0]+s[h-1]<<endl; } if(t) cout<<endl; } return 0; }
相关文章推荐
- UVA 10148 Advertisement 贴广告的艺术 贪心 区间选点
- uva 10716 Evil Straw Warts Live(贪心回文串)
- UVA-12545 Bits Equalizer (贪心)
- UVa 1467 Installations 解题报告(贪心)
- UVA - 11134 - Fabled Rooks(贪心)
- UVALive 4225 Prime Bases 贪心
- UVa 1149 - Bin Packing(贪心+二分查找)
- UVA 11691 - Allergy Test(状压dp+贪心)
- UVA - 12545 Bits Equalizer (贪心)
- UVA 1153 Keep the Customer Satisfied 顾客是上帝 经典贪心 (SWE 2005)
- uva 1450 - Airport(贪心+二分)
- uva 1149:Bin Packing(贪心)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- UVA 1555 - Garland(推公式,贪心)
- uva 1534 - Taekwondo(dp+贪心)
- uva 10670 Work Reduction (贪心)
- uva11292贪心基础题目
- uva 714 - Copying Books(贪心 最大值最小化 二分)
- UVALive 3177 && uva 1335 Beijing Guards (贪心+二分)
- uva 1442 - Cav(贪心)