UVA - 10132 File Fragmentation(贪心)

Question 2: File Fragmentation

The Problem

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


题目大意:
你的朋友端着一个盘子,上面有N份相同的文件,但是不小心盘子摔倒地上了,每个文件都摔成两份。现在用01组成的字符串来表示原来的文件和碎片,现在输入所有的碎片,根据这些碎片求出原文件。


解析:贪心题,首先按照字符串的长度进行升序排序,
原字符串 = 最短的字符串 + 最长的字符串。
最多有8个字符串
因为最长的字符串和最短的字符串,都是至多两个。
让后最短的字符串和最长的字符串,还可以前后排列。
所以最多8种可能。


然后判断这些字符串,如果所有的字符串是该字符串的子字符串,就输出该字符串。

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int cnt;
string file[150];
int maxlen,minlen;
bool cmp(string a,string b) {
	return a.size() < b.size();
}
bool judge(string str) {
	for(int i = 0; i < cnt; i++) {
		if(str.find(file[i]) == string::npos) {
			return false;
		}
	}
	return true;
}
void solve() {
	string target;
	for(int i = 0; file[i].size() == minlen; i++) {
		for(int j = cnt-1; file[j].size() == maxlen; j--) {
			for(int dir = 0; dir < 2; dir++) {
				switch(dir) {
					case 0:
						target = file[i] + file[j];
						break;
					case 1:
						target = file[j] + file[i];
						break;
				}
				if(judge(target)) {
					cout << target << endl;
					return ;
				}
			}
		}
	}
}
int main() {
	int t;
	cin >> t;
	cin.get();cin.get();
	while(t--) {
		for(cnt = 0;; cnt++) {
			getline(cin,file[cnt]);
			if(file[cnt] == "") {
				break;
			}
		}
		sort(file,file+cnt,cmp);
		maxlen = file[cnt-1].size();
		minlen = file[0].size();
		solve();
		if(t) {
			cout << endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UDP分片是指将一个过大的UDP数据报分割成多个IPv4片段的过程。UDP分片卸载允许设备将超大的UDP数据报分割成多个IPv4片段。与TCP分段卸载的要求类似,但是对于分片后的IPv4片段,其IPv4标识符不应该递增,因为它们属于同一个UDP数据报的分片。\[1\] 关于UDP分片卸载的更多信息可以参考文档\[2\]。在链路层中,有一个最大传输单元(MTU)的概念,它限制了数据帧的最大长度。不同网络类型的MTU值不同,例如以太网的MTU是1500字节。当IP层需要传输的数据包长度超过MTU时,就需要对数据包进行分片操作,使每个分片的长度小于或等于MTU。UDP分片就是在这种情况下发生的。\[3\] 总结来说,UDP分片是将超大的UDP数据报分割成多个IPv4片段的过程,以适应链路层的MTU限制。每个分片都包含有关它们属于同一个UDP数据报的信息。 #### 引用[.reference_title] - *1* *2* [Kernel: net: udp: ufo,UDP fragment offload, UDP分片脱负](https://blog.csdn.net/qq_36428903/article/details/126394978)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [UDP可靠传输(KCP))](https://blog.csdn.net/asdaqqwc/article/details/122385671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值