UVA 10132 File Fragmentation

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值