UVa Problem Solution: 10132 - File Fragmentation

本文介绍了一种通过让文件碎片“投票”来决定每个比特位应该是0还是1的方法,以此来复原原始文件内容。该算法确保了至少有一半以上的投票结果是正确的,从而能够高效准确地重构文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


I solve this problem in a manner that seems not straight forward. I let the fragments to "vote" on each bit if it is '0' or '1'. Specifically, for each fragment, it is either the head or tail of the file. We let it to vote as both. Thus, for each bit, there are exactly as many votes as the number of fragments, which is twice the number of files. Within these votes, we know that there are at least the number of files votes that are correct, since for each fragment, it has voted once as the correct part. Therefore, the number of correct votes is greater than or equal to the number of wrong votes. With the vote results in hand, we can reproduce the content of the original file easily.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10132 C++ "File Fragmentation" */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10. using namespace std;
  11.      
  12. int const filecount = 150;
  13. int const filesize = 300;
  14. void recover_file(char fragments[][filesize], int nfragments, char file[])
  15. {
  16.   int votes[filesize][2] = {{0}};
  17.   int lens[filecount*2] = {0};
  18.   int len = 0;
  19.   for (int i = 0; i < nfragments; ++i) {
  20.     for (int j = 0; fragments[i][j] != '/0'; ++j) {
  21.       ++votes[j][fragments[i][j] - '0'];
  22.       ++lens[i];
  23.       ++len;
  24.     }
  25.   }
  26.   len = len * 2 / nfragments;
  27.   for (int i = 0; i < nfragments; ++i) {
  28.     for (int j = 0; fragments[i][j] != '/0'; ++j) {
  29.       ++votes[len-lens[i]+j][fragments[i][j] - '0'];
  30.     }
  31.   }
  32.   for (int i = 0; i < len; ++i) {
  33.     file[i] = votes[i][0] > votes[i][1] ? '0' : '1';
  34.   }
  35. }
  36.           
  37. int main(int argc, char *argv[])
  38. {
  39. #ifndef ONLINE_JUDGE
  40.   filebuf in, out;
  41.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  42.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  43. #endif
  44.   int ncases;
  45.   cin >> ncases;
  46.   cin.ignore(2048, '/n').ignore(2048, '/n');
  47.   while (ncases-- > 0) {
  48.     char fragments[filecount*2][filesize] = {{0}};
  49.     int nfragments = 0;
  50.     while (cin.getline(fragments[nfragments], filesize) && 
  51.            fragments[nfragments][0] != '/0') {
  52.       ++nfragments;
  53.     }
  54.     char file[filesize] = {0};
  55.     recover_file(fragments, nfragments, file);
  56.     cout << file << '/n';
  57.     if (ncases > 0) cout << '/n';
  58.   }
  59.   return 0;
  60. }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值