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.
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.
1 011 0111 01110 111 0111 10111
01110111
题意:原本有很多个一样的长串,每个长串都分成了两个短串但是分割的位置不一定相同即两个短串不一定是平分的(就是输入中的那些串)。你要把这偶数个短串拼回那个长串(即2N个短串拼回N个相同的长串),如果有多种可能,任意一种即可,然后输出这个长串
思路:输入有点恶心,首先是case数,然后一个空行,然后再输入信息,然后又以一个空行结束该组数据,如果是最后一组数据了,不以空行结束,而是直接以EOF结束。输出,case之间有一个空行,最后一个case后面不要加空行
每组case的信息,就是N行的串,串中只有01,串的个数一定是偶数。
数据不大,144,直接暴力。首先按短串的长度升序排序,然后我们知道,长串的长度 = 最短的短串长度 + 最长的短串的长度。所以我们枚举最短的串和最长的串来组成一个长串,以这个长串也检验所有的组合,能成功的话立马输出。
代码写的超级乱。。ORZ。。
#include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<string> using namespace std; vector<string> vec; int tag[500]; bool cmp(string str1,string str2) { return str1.size()<str2.size(); } bool judge(string s) { memset(tag,0,sizeof(tag)); for(int i=0;i<vec.size();i++) { if(tag[i]==0) { int flag=0; for(int j=0;j<vec.size();j++) { if((vec[i]+vec[j]==s||vec[j]+vec[i]==s)&&tag[j]==0&&j!=i) { flag=1; tag[i]=tag[j]=1; break; } } if(flag==0) return false; } } for(int i=0;i<vec.size();i++) if(tag[i]==0) return false; return true; } int main() { int num; cin>>num; string str; getline(cin,str); getline(cin,str); while(num--) { vec.clear(); if(num>0) { while(getline(cin,str)&&str!="") { vec.push_back(str); } } else if(num==0) { while(getline(cin,str)) { vec.push_back(str); } } sort(vec.begin(),vec.end(),cmp); int len=vec.size(); int minlen=vec[0].size(),maxlen=vec[len-1].size(); int minpos,maxpos,i,j,k; for(i=0;i<len;i++) { if(vec[i].size()>minlen) { minpos=i; break; } } for(i=len-1;i>=0;i--) { if(vec[i].size()<maxlen) { maxpos=i; break; } } if(maxlen==minlen) { minpos=0; maxpos=vec.size()-1; } int flag=0; for(i=0;i<=minpos;i++) { for(j=len-1;j>=maxpos;j--) { if(i!=j) { string str1=vec[i]+vec[j]; string str2=vec[j]+vec[i]; if(judge(str1)) { cout<<str1<<endl; flag=1; break; } if(judge(str2)) { cout<<str2<<endl; flag=1; break; } } } if(flag) break; } if(num) cout<<endl; } return 0; }