#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
// 数据块编号 磁盘编号 保存的数据
char str[128][8][128];
char mmp[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }, ch;
int d, s, b, cnt = 0;
bool IsComplete(int id, int flag,string &ret) {
//周期性 作为校验块
int t = id % d;
for (int i = 0; i < s; ++i) {
int cnt = 0,temp,index = -1,base = 0;
for (int j = 0; j < d; ++j) {
temp = str[id][j][i];
if (temp == 'x') { index = j; ++cnt; if (cnt > 1) return false; continue; }
base ^= (temp - '0');
}
//判断是否有多个丢失数据
if (index != -1) {
str[id][index][i] = (base ^ flag) + '0';
}
//没有丢失数据 是否检验成功
else if (base != flag) return false;
}
for (int i = 0; i < d; ++i) {
//不是检验块才添加
if(i == t) continue;
ret += str[id][i];
}
return true;
}
string GetHex(string& input) {
//转成16进制输出
string result = "";
int num = 0;
for (int i = 0; i < input.size(); ++i) {
if (i && i % 4 == 0) {
result.push_back(mmp[num]);
num = 0;
}
num = (num << 1) | (input[i] - '0');
}
if (input.size() % 4 != 0) {
int t = 4 - input.size() % 4;
num = num << t;
}
result.push_back(mmp[num]);
return result;
}
int main()
{
while (cin >> d && d) {
cin >> s >> b;
string ret = "";
bool isok = true;
cin >> ch;
int flag = ch == 'E' ? 0 : 1;
//一个磁盘一个磁盘的填充
for (int j = 0; j < d; ++j)
for (int i = 0; i < b; ++i) {
for (int k = 0; k < s; ++k)
cin >> str[i][j][k];
str[i][j][s] = '\0';
}
for (int i = 0; i < b; ++i) {
if (IsComplete(i, flag, ret) == false) { isok = false; break; }
}
if (isok) cout << "Disk set " << ++cnt << " is valid, contents are: " << GetHex(ret) << endl;
else cout << "Disk set " << ++cnt << " is invalid." << endl;
}
return 0;
}
习题4-7(uva-509)
最新推荐文章于 2021-04-05 14:15:13 发布