http://uva.onlinejudge.org/external/101/10132.pdf
简单来说就是有很多一样的被切成两片,现在给你碎片,问原来的样子。
按长度排个序,把最短的和最长的拼起来试一试就好了。输入处理要弄好
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<functional>
#include<stack>
using namespace std;
const int maxn = 305;
int t, tot, f[maxn];
string s[maxn], a[4];
bool ff;
char cc[100];
bool cmp(const string&a, const string&b)
{
if (a.size() == b.size()) return a < b;
else return a.size() < b.size();
}
int main(){
cin >> t; gets(cc); gets(cc);
while (t--)
{
tot = -1;
while (gets(cc))
{
if (cc[0] == '\0') break; s[++tot] = "";
for (int i = 0; i < strlen(cc); i++) s[tot] = s[tot] + cc[i];
}
sort(s, s + ++tot, cmp);
a[0] = s[0] + s[tot - 1];
a[1] = s[tot - 1] + s[0];
a[2] = s[1] + s[tot - 2];
a[3] = s[tot - 2] + s[1];
for (int i = 0; i < 4; i++)
{
memset(f, 0, sizeof(f)); ff = true;
for (int j = 0; j < tot;j++)
for (int k = 0; k < tot;k++)
if (j != k&&!f[j]&&!f[k])
if (s[j] + s[k] == a[i] || s[k] + s[j] == a[i]) f[j] = f[k] = 1;
for (int j = 0; j < tot; j++) if (f[j] == 0) ff = false;
if (ff) { cout << a[i] << endl; break; }
}
if (t) printf("\n");
}
return 0;
}