【CodeForces 1251B --- Binary Palindromes】

Description

A palindrome is a string t which reads the same backward as forward (formally, t[i]=t[|t|+1−i] for all i∈[1,|t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.

You have n binary strings s1,s2,…,sn (each si consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.

Formally, in one move you:

choose four integer numbers x,a,y,b such that 1≤x,y≤n and 1≤a≤|sx| and 1≤b≤|sy| (where x and y are string indices and a and b are positions in strings sx and sy respectively),
    swap (exchange) the characters sx[a] and sy[b].

What is the maximum number of strings you can make palindromic simultaneously?

Input

The first line contains single integer Q (1≤Q≤50) — the number of test cases.

The first line on each test case contains single integer n (1≤n≤50) — the number of binary strings you have.

Next n lines contains binary strings s1,s2,…,sn — one per line. It’s guaranteed that 1≤|si|≤50 and all strings constist of zeroes and/or ones.

Output

Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.

Sample Input

4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111

Sample Output

1
2
2
2

Note

In the first test case, s1 is palindrome, so the answer is 1.

In the second test case you can’t make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let’s make s1=0110, s2=111111 and s3=010000.

In the third test case we can make both strings palindromic. For example, s1=11011 and s2=100001.

In the last test case s2 is palindrome and you can make s1 palindrome, for example, by swapping s1[2] and s1[3].

解题思路:根据题目可以得到一共有四种情况,情况一 1:奇数 0:奇数,情况二 1:偶数 0:偶数,情况三 1:奇数 0:偶数,情况四 1:偶数 0:奇数。可以看出四种情况中只有情况一不是回文,如果要使情况一变成回文,只有情况二、三与情况一交换1个1或1个0,如果全部字符串都是情况一、二,当1和0的个数都是奇数的时候,那么最后将有一串无法变成回文。如果全部字符串都是情况一,当1和0的个数都是奇数的时,那么最后将有一串无法变成回文。(奇数=奇数+偶数),所以是一个字符串1的个数为奇数,0的个数为偶数。

AC代码:

#include <iostream>
#include <string>
using namespace std;
int a[];
string str;
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int sum=,flag=;
a[]=a[]=;
for(int i=;i<n;i++)
{
cin>>str;
int len=str.size();
if(len%)
flag=;
for(int j=;j<len;j++)
a[str[j]-'']++;
}
if(a[]%&&a[]%&&!flag)
n--;
cout<<n<<endl;
}
return ;
}

CodeForces 1251B --- Binary Palindromes的更多相关文章

  1. Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

    链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...

  2. Codeforces #662C Binary Table

    听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...

  3. Codeforces 838A - Binary Blocks(二维前缀和+容斥)

    838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...

  4. [CodeForces 663E] - Binary Table(FWT)

    题目 Codeforces 题目链接 分析 大佬博客,写的很好 本蒟蒻就不赘述了,就是一个看不出来的异或卷积 精髓在于 mask对sta的影响,显然操作后的结果为mask ^ sta AC code ...

  5. [Codeforces]663E Binary Table

    某变换好题.不过听说还有O(2^n*n^2)DP的…… Description 给定一个n*m的01矩阵,你可以选择对任意行和任意列取反,使得最终“1”的数量尽量少. Input 第一行两个整数n,m ...

  6. Codeforces.662C.Binary Table(状压 FWT)

    题目链接 \(Description\) 给定一个\(n\times m\)的\(01\)矩阵,你可以选择一些行和一些列并将其中所有的\(01\)反转.求操作后最少剩下多少个\(1\). \(n\le ...

  7. CodeForces 838A Binary Blocks(前缀和)题解

    题意:给你个n*m的矩阵,要求你找到一个k,k > 1,使得矩阵可以分为很多k * k的小正方形,然后进行操作把每个小正方形都变为0或1,问你怎样使操作数最小. 思路:随便暴力不可取,显然你每次 ...

  8. CodeForces 688B Lovely Palindromes (水题回文)

    题意:给一个数n,让你找出长度为偶数,并且是第 n 个回文数. 析:你多写几个就知道了,其实就是 n,然后再加上n的逆序,不过n有点大,直接用string 好了. 代码如下: #include < ...

  9. CodeForces - 662C Binary Table (FWT)

    题意:给一个N*M的0-1矩阵,可以进行若干次操作,每次操作将一行或一列的0和1反转,求最后能得到的最少的1的个数. 分析:本题可用FWT求解. 因为其0-1反转的特殊性且\(N\leq20\),将每 ...

随机推荐

  1. unity里framebuffer fetch相关问题

    https://docs.unity3d.com/Manual/SL-PlatformDifferences.html 开了framebuffer fetch 如果你再在里面sample 那张rt ( ...

  2. filebeat configure

    docker run -d --rm -v ./filebeat.yml:/usr/share/filebeat/filebeat.yml -v /var/log:/var/log  docker.e ...

  3. @Valid验证

    @Valid注解用于校验,所属包为:javax.validation.Valid. ① 首先需要在实体类的相应字段上添加用于充当校验条件的注解,如:@Min,如下代码(age属于User类中的属性): ...

  4. jmeter请求参数中文乱码,解决方法

  5. linux系统编程--文件IO

    系统调用 什么是系统调用: 由操作系统实现并提供给外部应用程序的编程接口.(Application Programming Interface,API).是应用程序同系统之间数据交互的桥梁. C标准函 ...

  6. 【原创】洛谷 LUOGU P3371 【模板】单源最短路径

    P3371 [模板]单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出 ...

  7. 「BZOJ 2653」middle「主席树」「二分」

    题意 一个长度为\(n\)的序列\(a\),设其排过序之后为\(b\),其中位数定义为\(b[n/2]\),其中\(a,b\)从\(0\)开始标号,除法取下整.给你一个长度为\(n\)的序列\(s\) ...

  8. 利用Python构建时间序列模型解决实际问题的正确姿势

    要本着应用到实际工作中目的去学时间序列分析,才能深入浅出的学会,不要纠结于理论,只听我的,我有信心说明白. 本章内容 趋势分析 序列分解 序列预测 序列分解 统计学基础铺垫 划分 时间序列按照季节性划 ...

  9. Elasticsearch学习笔记-Delete By Query API

    记录关于Elasticsearch的文档删除API的学习 首先官网上Document APIs介绍了 Delete API 和Delete By Query API. Delete API可以通过指定 ...

  10. vue实战教程

    转载自 https://www.cnblogs.com/sunsets/p/7760454.html