Question 2: 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 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个相同的文件,将它们进行两段分离,形成2n的文件,要求出原来的文件;
找出2n文件中中最长以及最短的文件,并把两者进行相加,得到原来文件的长度 len,将各自的字符串进行连接,
然后两个进行拼接,利用一个map的second来保存各自两个出现的次数;出现最多次数的文件,就是原来的文件;
记住题目数据的输入;空行以及回车键;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
#define N 500
string str[N];
map<string,int> m;
int main () {
char s[N];
int n; cin >> n;
getchar();
gets(s);
while (n--) {
int k = 0;
while (gets(s)) {
if (s[0] == '\0') break;
str[k++] = s;
}
int Min = 10000, Max = 0;
for (int i = 0; i < k; i++) {
if (Min > str[i].size()) Min = str[i].size();
if (Max < str[i].size()) Max = str[i].size();
}
int len = Min + Max;
map<string,int> ::iterator it;
m.clear();
for (int i = 0; i < k;i++)
for (int j = i+1; j < k;j++) {
string temp = str[i] + str[j];
if (temp.size() == len) {
it = m.find(temp);
if (it != m.end()) m[temp]++;
else m[temp] = 1;
}
temp = str[j] + str[i];
if (temp.size() == len) {
it = m.find(temp);
if (it != m.end()) m[temp]++;
else m[temp] = 1;
}
}
int flag = 0;
string ans;
for (it = m.begin(); it != m.end(); it++) {
if (it->second > flag) {
flag = it->second;
ans = it->first;
}
}
cout << ans << endl;
if (n) printf("\n");
}
return 0;
}