psbook solutions

সমস্যা ও সমাধান বই - ১ এর উপর অনুশীলন ( Practice code for Problem and Solutions Book - 1 by Mahbubul Hasan )

View the Project on GitHub

UVa 509 - RAID!

Commit Time 15 Oct 2017 14:44
#include <bits/stdc++.h>

using namespace std;

char disks[8][6410];
int d, s, b;
bool parity;

bool read() {
    char p;
    scanf("%d %d %d\n%c\n", &d, &s, &b, &p);
    if (d == 0) return 0;

    parity = (p == 'O');

    for (int i = 0; i < d; i++) {
        scanf("%s", disks[i]);
    }
    return 1;
}

bool valid() {
    for (int i = 0; i < s*b; i++) {
        int k = 0, lost = -1;
        for (int j = 0; j < d; j++) {
            char &c = disks[j][i];
            if (c == '1') k ^= 1;
            else if ( c == 'x') {
                if (lost != -1) return 0;
                lost = j;
            }
        }
        if ( lost == -1 && k != parity ) return 0;
        if (lost != -1) disks[lost][i] = (parity != k) + '0';
    }
    return 1;
}

void printData() {
    int n = 0, cnt = 0;
    for (int i = 0; i < b; i++) {
        for (int j = 0; j < d; j++) {
            if (j == i % d) continue;
            for (int k = 0; k < s; k++) {
                if (disks[j][i*s + k] == '1') n += 1;
                if(++cnt == 4) {
                    printf("%X", n);
                    n = 0;
                    cnt = 0;
                } else {
                    n <<= 1;
                }
            }
        }
    }
    if (cnt) {
        n <<= (3-cnt);
        printf("%X", n);
    }
    printf("\n");
}

int main() {
    int setno = 1;
    while ( read() ) {
        printf("Disk set %d is ", setno++);
        if ( valid() ) {
            printf("valid, contents are: ");
            printData();
        } else printf("invalid.\n");
    }
    return 0;
}
Status WA
Commit Time 15 Oct 2017 14:44

case = 1
while True:
    try:
        d, s, b = map(int, input().strip().split())
    except ValueError:
        break
    p = (0 if input() == 'E' else 1)
    data = []
    for i in range(d):
        data.append(list(input().strip()))

    valid = True
    for i in range(s*b):
        k = 0
        lost = -1
        for j in range(d):
            c = data[j][i]
            if c == '1':
                k ^= 1
            if c == 'x':
                if lost != -1:
                    valid = False
                    break
                else:
                    lost = j
        if not valid or (lost == -1 and k != p):
            valid = False
            break
        if lost != -1:
            data[lost][i] = '0' if k == p else '1'

    if not valid:
        print("Disk set", case, "is invalid.")
    else:
        content = ''
        for i in range(b):
            for j in range(d):
                if j != i % d:
                    content += ''.join(data[j][i*s:(i+1)*s])
        if len(content) % 4:
            content += '0'*(4 - (len(content) % 4))
        print("Disk set", case, "is valid, contents are:", hex(int(content, 2))[2:].upper())
    case += 1