CCF CSP 竞赛试题——损坏的RAID5(201903-3)

  • 需要处理大量输入输出的时候,得请出cin.tie(NULL)sync_with_stdio(false)俩兄弟。
  • 不能直接把缺的一整块给算出来,服气。太细了,考试时可能不会注意。
#include <iostream>
#include <cctype>
#include <vector>
using namespace std;

int char_to_int(char c) { return isdigit(c) ? c - '0' : c - 'A' + 10; }

char int_to_char(int x) { return x >= 10 ? x - 10 + 'A' : x + '0'; }

string exclusiveOr(const string& a, const string& b) {
    if (a.empty()) return b;
    string res;
    for (int i = 0; i < a.size(); ++i)
        res.push_back(int_to_char(char_to_int(a[i]) ^ char_to_int(b[i])));
    return res;
}

int main() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int n, s, l;
    cin >> n >> s >> l;
    vector<string> disks(n);
    int N; // 一个硬盘总字节数的两倍
    for (int i = 0; i < l; ++i) {
        int idx;
        string s;
        cin >> idx >> s;
        disks[idx] = s;
        N = s.length();
    }
    int totalNumOfBlocks = (N >> 3) * (n - 1); // 总的块数
    // if (n - 1 == l) { // 如果直接把缺的那一块整个给算出来,只有90分,TLE
    //     string res;
    //     int k;
    //     for (int i = 0; i < n; ++i) {
    //         if (disks[i].empty())
    //             k = i;
    //         else
    //             res = exclusiveOr(res, disks[i]);
    //     }
    //     disks[k] = res;
    // }
    int m;
    cin >> m;
    while (m--) {
        int query;
        cin >> query;
        if (query >= totalNumOfBlocks) {
            cout << "-" << endl;
            continue;
        }
        int k = query / (s * (n - 1)); // 第k层
        int start = n - 1 - k % n;     // 校验盘的盘号
        int diskIdx = (start + query % (s * (n - 1)) / s + 1) % n; // 待查询数据所在的盘号
        int blockIdx = k * s + query % s;  // 待查询数据所在的块号(相对于所在硬盘)
        if (disks[diskIdx].empty()) {
            if (n - 1 == l) {
                string res;
                for (int i = 0; i < n; ++i) if (!disks[i].empty())
                    res = exclusiveOr(res, disks[i].substr(blockIdx << 3, 8));
                cout << res << endl;
            } else {
               cout << "-" << endl;
            }
        } else {
            cout << disks[diskIdx].substr(blockIdx << 3, 8) << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值