UVa 10132 File Fragmentation (想法题)

本文详细探讨了在实验室事故中,文件碎片通过ASCII字符转换后,如何通过算法重组原始文件的过程。包括输入输出规范、算法思路及完整代码实现。

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


### 内部碎片的概念及其与稳定存储的关系 #### 内部碎片的定义 内部碎片(Internal Fragmentation)是指在内存管理或存储系统中,分配给某个进程或数据结构的存储空间比实际所需的空间大,导致部分空间未被使用的情况。这种现象通常发生在固定大小的块分配策略中,例如操作系统中的页分配机制[^2]。由于分配单元的大小通常是固定的(如页面大小为4KB),如果应用程序请求的内存小于该固定大小,则剩余的空间将无法被其他进程利用,从而形成内部碎片。 #### 稳定存储的定义 稳定存储(Stable Storage)是一种即使在系统崩溃或其他故障情况下也能保证数据持久性的存储介质。它通常用于实现数据库系统的事务日志和恢复机制。例如,在MySQL的InnoDB存储引擎中,双写缓冲区(Doublewrite Buffer)是一种用于确保数据完整性和一致性的稳定存储技术[^1]。通过将数据先写入一个临时区域,然后再写入最终目标位置,可以避免因系统崩溃而导致的部分写入问。 #### 内部碎片与稳定存储的关系 在稳定存储的设计中,内部碎片可能成为一个需要考虑的因素。例如,在实现双写缓冲区时,数据被写入到特定大小的块中。如果数据的实际大小小于块的大小,则可能会产生内部碎片。这不仅浪费了存储空间,还可能导致性能下降,尤其是在频繁写入小块数据的情况下。 此外,某些硬件设备(如Fusion-io设备)支持原子写操作,可以在一定程度上减少对双写缓冲区的需求,从而降低因块分配策略导致的内部碎片[^1]。然而,这种优化仅适用于特定硬件环境,并且需要适当的配置(如设置`innodb_flush_method`为`O_DIRECT`)以充分发挥其优势。 #### 示例代码:模拟内存分配中的内部碎片 以下代码展示了如何模拟固定大小块分配策略下的内部碎片现象: ```python class MemoryAllocator: def __init__(self, block_size): self.block_size = block_size self.allocated_blocks = [] def allocate(self, request_size): allocated_block = self.block_size internal_fragmentation = allocated_block - request_size self.allocated_blocks.append((allocated_block, internal_fragmentation)) return internal_fragmentation # 示例:分配不同大小的请求 allocator = MemoryAllocator(block_size=4096) # 每个块大小为4KB fragmentations = [allocator.allocate(size) for size in [1024, 2048, 3072]] print("Internal Fragmentations:", fragmentations) ``` 运行上述代码后,将输出每次分配请求所产生的内部碎片大小。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值