Parity Game题解

· · 题解

前言:

题目传送门

题目分析:

首先统计出 a,b 中字符 1 的数量 suma,sumb

然后计算出 \operatorname{parity}(a):直接循环统计 1 的个数 cnt,然后将 cnt 取余 2,再将现在的 cnt 转换成字母连接在 a 后面。

如果 \operatorname{parity}=1,那么 a 就不能加 1 了,也就是只能使用操作 2,那么如果 suma \leq sumb,那么输出 YES,否则输出 NO

如果 \operatorname{parity}(a)=0,那么 a 可以用 1\operatorname{parity}(a),即:suma \leq sumb-1 输出 YES,反之输出 NO

代码:

#include <bits/stdc++.h>
using namespace std;
string a,b;
int main()
{
    cin>>a>>b;
    int suma=0,sumb=0;
    for(int i=0;a[i];i++)
    if(a[i]=='1') suma++;
    for(int i=0;b[i];i++)
    if(b[i]=='1') sumb++;
    if(suma%2==0)
    if(suma>=sumb) cout<<"YES";
    else cout<<"NO";
    if(suma%2==1)
    if(suma+1>=sumb) cout<<"YES"; 
    else cout<<"NO";
    return 0;//完结散花 
}