UVa 10132 File Fragmentation (想法题)

10132 - File Fragmentation

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1073

Your friend, a biochemistry major, tripped while carrying a tray of computer files through the lab. All of the files fell to the ground and broke. Your friend picked up all the file fragments and called you to ask for help putting them back together again.

Fortunately, all of the files on the tray were identical, all of them broke into exactly two fragments, and all of the file fragments were found. Unfortunately, the files didn't all break in the same place, and the fragments were completely mixed up by their fall to the floor.

You've translated the original binary fragments into strings of ASCII 1's and 0's, and you're planning to write a program to determine the bit pattern the files contained.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Input will consist of a sequence of ``file fragments'', one per line, terminated by the end-of-file marker. Each fragment consists of a string of ASCII 1's and 0's.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Output is a single line of ASCII 1's and 0's giving the bit pattern of the original files. If there are 2N fragments in the input, it should be possible to concatenate these fragments together in pairs to make N copies of the output string. If there is no unique solution, any of the possible solutions may be output.

Your friend is certain that there were no more than 144 files on the tray, and that the files were all less than 256 bytes in size.

Sample Input

1

011
0111
01110
111
0111
10111

Sample Output

01110111

学英语=v=~

1. all of the files on the tray were identical, all of them broke into exactly two fragments.

文件盒里的所有文件都是完全一样的,所有文件都恰好碎成了两部分。

2. If there is no unique solution, any of the possible solutions may be output.

如果没有唯一解,任何可能的答案都可以是输出。(但是题目不是Special Judge,所以只有一种文件的拼法)


思路:最短的碎片长度+最长的碎片长度=原文件的长度,所以枚举排序后的s[0]和最长的碎片拼成的文件,去比较s[1]和其他碎片拼成的文件,看是否相同。


完整代码:

/*0.015s*/

#include<cstdio>
#include<cstring>
#include<cstdlib>

char s[160][260], a[260], b[260];
int n, len;

int cmp(const void* a, const void* b)
{
	return strlen((char*)a) < strlen((char*)b);
}

bool judge()
{
	for (int i = n - 1; i >= 0; --i)
	{
		if (strlen(s[1]) + strlen(s[i]) != len) continue;
		strcpy(b, s[1]);
		strcat(b, s[i]);
		if (strcmp(a, b) == 0) return true;
		///如果本来当前所拼的文件a就是错(不是原文件)的,那我们是不可能拼到另一个一样错的文件
		strcpy(b, s[i]);
		strcat(b, s[1]);
		if (strcmp(a, b) == 0) return true;
	}
	return false;
}

int main()
{
	int T, i, min, max;
	scanf("%d\n", &T);
	while (T--)
	{
		n = 0;
		while (gets(s[n]) && s[n][0]) ++n;
		qsort(s, n, sizeof(s[0]), cmp);
		min = strlen(s[0]);
		max = strlen(s[n - 1]);
		len = min + max;
		for (i = n - 1; i >= 0 && (int)strlen(s[i]) == max; --i)///找那些最长的碎片去匹配s[0]
		{
			strcpy(a, s[0]);
			strcat(a, s[i]);
			if (judge()) break;
			strcpy(a, s[i]);
			strcat(a, s[0]);
			if (judge()) break;
		}
		puts(a);
		if (T) putchar(10);
	}
	return 0;
}


你可能感兴趣的:(C++,ACM,uva)