【习题 4-7 UVA

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

如果一行里面某位有>1个x 那么是invalid的。 没有x的话。 可以分析以下(设输入的标准Even为0,然后Odd为1) (列出所有情况分析后会发现.) 那么必须满足标准^这一列该位的亦或和==0 x只有1个的情况的话。也应该满足这个,所以就能根据上面这个求出x是啥啦。 末尾添加0 是添加(4-len%4)个...不是len%4个。。 (如果len%4!=0的话)

【代码】

#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;

const int N = 1e2;

int m,ss,n;
string a[N+10][N+10];
char ju[3];
int _ju;

string check(){
    vector<int> v;v.resize(m);
    rep1(i,1,n)
        rep1(p,0,ss-1){
            int _xor = 0;
            rep1(j,1,m){
                if (a[i][j][p]=='x'){
                    v[j-1] = -1;
                }else{
                    v[j-1]=a[i][j][p]-'0';
                    _xor ^= v[j-1];
                }
            }
            int idx = -1,cnt = 0;
            rep1(j,1,m)
                if (v[j-1]==-1){
                    idx = j;
                    cnt++;
                }
            if (cnt>1) return " is invalid.";
            if (cnt==0){
                //假设其他东西的亦或值为x
                //验证为的值为a
                //如果ju[0]=='E' 当成0
                //那么x为偶数的话,a的值要为0
                //如果x为奇数的话,a的值要为1

                //如果ju[0]=='O' 当成1
                //那么x为偶数的话,a的值要为1
                //如果x为奇数的话,a的值要为0
                //所以只要看看所有东西的亦或值^ju[0]==0(Even为0,Odd为1)的话
                //就是合法的
                //所以那个Parity在这一行的哪里都没关系,不用知道在哪里.
                if (_xor^_ju!=0) return " is invalid.";
                //如果运行到这,说明这一行的第p位是ok的
            }else{
                //把对应的位置改成__xor^ju就ok了。
                //这样xorall^ju就为0了
                a[i][idx][p]=_xor^_ju+'0';
            }

        }
    string temp = "";
    int idx = 1;
    //cout<<endl;
    rep1(i,1,n){
        rep1(j,1,m){
            if (j!=idx){
                temp = temp + a[i][j];
            }
            //cout<<a[i][j]<<" ";
        }
        //cout<<endl;
        idx++;
        if (idx>m) idx = 1;
    }
    int len = temp.size();
    if (len%4!=0){
        rep1(i,1,4-len%4) temp+="0";
    }
    len = temp.size();
    string ans = " is valid, contents are: ";
    int hex=0;
    //cout<<temp<<endl;
    rep1(i,1,len){
        hex=hex*2+temp[i-1]-'0';
        if (i%4==0){
            //cout<<hex<<" ";
            if (hex<=9){
                char key = hex+'0';
                ans = ans+(char)(hex+'0');
            }else{
                char key = hex-10+'A';
                ans = ans+key;
            }
            hex = 0;
        }
    }
    return ans;
}

int main(){
    //freopen("/home/ccy/rush.txt","r",stdin);
    //freopen("/home/ccy/rush_out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int kase = 0;
    while (cin >> m){
        if (m==0) break;
        cin >> ss >> n;
        cin >> ju;
        if (ju[0]=='E') _ju = 0;else _ju = 1;
        rep1(i,1,m){
            //第i个硬盘.
            string s;cin >> s;
            string temp="";
            for (int j = 1,idx = 1;j <= n;idx++){
                temp+=s[idx-1];
                if (idx%ss==0){
                    a[j][i] = temp;
                    temp = "";
                    j++;
                }
            }
        }
        cout<<"Disk set "<<++kase<<check()<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/9882051.html