10132 - File Fragmentation

题意:n个相同的文件摔成2*n个文件碎片,而且每一个文件碎成二片!求原文件的序列,只有1与0

算法:贪心+暴力

连接:点击打开链接

#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;

string s[300];
int vis[300];
string tmps;
int len;
int num;

bool cmp ( string a, string b )
{
	return a.length() > b.length();
}

bool judge ( string str )
{
	for ( int i = 0; i < ( num + 1 ) / 2; i++ )
	{
		vis[i] = 1;
		bool found = false;
		for ( int j = num - 1; j > i; j-- )
		{
			if ( vis[j] == 0 && s[i] + s[j] == str )
			{
				vis[j] = 1;
				found = true;
				break;
			}
		}
		if ( !found )
			return false;
	}
	return true;
}

void solve ( )
{
	num = 0;
	char str[300];
	while ( gets ( str ) )
		if ( str[0] )
			s[num++] = str;
		else
			break;
	sort ( s, s + num, cmp );
	len = s[0].length() + s[num-1].length();
	int len1 = s[num-1].length();
	bool found = false;
	for ( int i = num - 1; i >= 0 && s[i].length () == len1; i-- )
	{
		tmps = s[0] + s[i];
		memset ( vis, 0, sizeof vis );
		if ( judge ( tmps ) )
		{
			found = true;
			cout << tmps << endl;
			return ;
		}
	}
	if ( !found )
		cout << s[0] + s[num-1] << endl;
}

int main ( )
{
	int n;
	scanf ( "%d\n", &n );
	while ( n-- )
	{
		solve ( );
		if ( n )
			puts ( "" );
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值