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
然后判断这些字符串,如果所有的字符串是该字符串的子字符串,就输出该字符串。
#include <string> #include <iostream> #include <algorithm> using namespace std; int cnt; string file[150]; int maxlen,minlen; bool cmp(string a,string b) { return a.size() < b.size(); } bool judge(string str) { for(int i = 0; i < cnt; i++) { if(str.find(file[i]) == string::npos) { return false; } } return true; } void solve() { string target; for(int i = 0; file[i].size() == minlen; i++) { for(int j = cnt-1; file[j].size() == maxlen; j--) { for(int dir = 0; dir < 2; dir++) { switch(dir) { case 0: target = file[i] + file[j]; break; case 1: target = file[j] + file[i]; break; } if(judge(target)) { cout << target << endl; return ; } } } } } int main() { int t; cin >> t; cin.get();cin.get(); while(t--) { for(cnt = 0;; cnt++) { getline(cin,file[cnt]); if(file[cnt] == "") { break; } } sort(file,file+cnt,cmp); maxlen = file[cnt-1].size(); minlen = file[0].size(); solve(); if(t) { cout << endl; } } return 0; }