其他分享
首页 > 其他分享> > CodeForces - 1251B - Binary Palindromes(思维)

CodeForces - 1251B - Binary Palindromes(思维)

作者:互联网

题目链接:https://vjudge.net/problem/CodeForces-1251B

题目大意:给你若干个01串,你可以交换任意一对字符,可以在同一个串,也可以在不同串

因为题目的限制很自由,所有我们只要判断奇偶就行了.注释应该写的很详细了

#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<char, int> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e3+10;
int main(void) {
    int t;
    cin >> t;
    while(t--) {
        int n, n0 = 0, n1 = 0, odds = 0; string s;
        cin >> n;
        for (int i = 0; i<n; ++i) { 
            cin >> s;
            int size = s.size();
            if (size&1) ++odds; //统计奇数串数目
            for (auto ch : s) { //统计0和1的数目
                if (ch=='1') ++n1;
                else if (ch=='0') ++n0;
            }
        }
        if (odds==1 && n1&1 && n0&1) cout << n-1 << endl; 
        //如果只有1个奇数串,则只能有一种数字是奇数
        else if (odds&1 && ((~n0&1 && ~n1&1) || (n0&1 && n1&1))) cout << n-1 << endl; 
        //如果奇数串的数量是奇数而且大于1,那么只有在只有1种字符数量为奇数的时候可行
        else if (!odds && ((n1&1)||(n0&1))) cout << n-1 << endl;
        //如果没有奇数串那么两种数字都不能出现奇数
        else if (~odds&1 && ((n1&1 && ~n0&1) || (~n1&1 && n0&1))) cout << n-1 << endl;
        //如果奇数串的数量不为1且大于0, 那么只要两种数字的数量都是偶数就行
        else cout << n << endl;
    }
    return 0;
}

 

标签:Binary,typedef,ch,const,int,CodeForces,Palindromes,include,define
来源: https://www.cnblogs.com/shuitiangong/p/12437407.html