算法竞赛入门经典第四章习题4-7 RAID! UVA - 509

https://vjudge.net/problem/UVA-509
坑点:每列x可能存在大于1个

#include
#include
#include
#pragma warning(disable:4996)
using namespace std;
const int maxn = 100;
string AA[10];
int main() {
#ifdef _DEBUG
    freopen("in", "rb", stdin);
    freopen("out", "wb", stdout);
#endif // _DEBUG
    int d, s, b,mode;
    string _mo;
    int cnt=0;
    while (cin >> d && d) {
        bool _valid = true;
        cin >> s >> b >> _mo;
        if (_mo == "O") mode = 1;
        else mode = 0;
        for (int k = 1; k <= d; ++k) cin >> AA[k];
        int L = s * b,x,y;
        for (int i = 0; i < L; ++i) {
            int tt = mode;
            x = 0;
            for (int k = 1; k <= d; ++k)
                if (AA[k][i] == 'x')
                    if(!x)x = k, y = i;
                    else { _valid = false; break; }
                else tt ^= AA[k][i] - '0';
            if (x) AA[x][y] = tt+'0';
            else if (tt) { _valid = false; break; }
        }
        if (_valid) {
            int k=1;
            string data,ans;
            for (int i = 0; i < b;++i) {
                int m = i + s;
                for (int j = 1; j <= d; ++j)
                    if (j != k) data += string(AA[j], i*s, s);
                k = k % d + 1;
            }
            data += string((4 - data.size() % 4) % 4, '0');
            for (int i = 0; i < data.size(); i += 4) {
                char e[5];
                sprintf(e, "%X", bitset<4>(string(data,i, 4)).to_ulong());
                ans += e;
            }
            printf("Disk set %d is valid, contents are: %s\n", ++cnt, ans.c_str());
        }
        else
            printf("Disk set %d is invalid.\n",++cnt);
    }

}   

你可能感兴趣的:(算法竞赛入门经典第四章习题4-7 RAID! UVA - 509)