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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章