uva-10132-排序

题意:

有很多文件,碎成了俩片,问,原来的文件是什么,如果有多个答案,任意一个答案就行,输入2N个字符串,拼接成N个文件.

直接排序,正确的答案一定是某个长度最短的和某个最长的连在一起.

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <memory.h> namespace cc
{
using std::cin;
using std::cout;
using std::endl;
using std::move;
using std::sort;
using std::string; int cmp(const string &a, const string &b)
{ return a.length() < b.length();
} const int N = ; void solve()
{
int n;
cin >> n;
getchar();
getchar();
int tt = ;
while (n--)
{
if (tt != )
cout << endl;
++tt;
string strs[N];
int total = ; string s;
int totalLength = ;
while (getline(cin, s))
{
if (s.length() == )
break;
strs[total++] = s;
}
sort(strs, strs + total, cmp);
totalLength = strs[].size() + strs[total - ].size();
int vis[N];
for (int i = ;; i++)
{
memset(vis, , sizeof(vis));
string temp = strs[i] + strs[total - ];
int count = ;
//vis[i] = vis[total - 1] = 1;
for (int j = ; j < total; j++)
{
for (int k = ; k < total; k++)
{
if (vis[j] || vis[k] || k == j || strs[j].length() + strs[k].length() != totalLength)
continue;
if (strs[k] + strs[j] == temp || strs[j] + strs[k] == temp)
{
vis[j] = vis[k] = ;
++count;
break;
}
}
}
if (count == total / )
{
cout << temp << endl;
break;
}
else
{
memset(vis, , sizeof(vis));
string temp = strs[total - ] + strs[i];
count = ;
for (int j = ; j < total; j++)
{
for (int k = ; k < total; k++)
{
if (vis[j] || vis[k] || k == j || strs[j].length() + strs[k].length() != totalLength)
continue;
if (strs[k] + strs[j] == temp || strs[j] + strs[k] == temp)
{
vis[j] = vis[k] = ;
++count;
break;
}
}
}
if (count == total / )
{
cout << temp << endl;
break;
}
}
}
}
} } // namespace cc int main()
{
#ifndef ONLINE_JUDGE
freopen("/Users/caicai/in", "r", stdin);
#endif // !ONLINE_JUDGE cc::solve(); #ifndef ONLINE_JUDGE while (true)
;
#endif // !ONLINE_JUDGE
return ;
}
上一篇:从省市区多重级联想到的,react和jquery的差别


下一篇:IDEA使用Maven搭建SSM框架