【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. [HBase]region compaction流程

  2. 我对line-height及vertical-align的一点理解

    张鑫旭老师在文章<我对CSS vertical-align的一些理解与认识(一)>中提到: vertical-align:middle属性的表现与否,仅仅与其父标签有关,至于我们通常看到的 ...

  3. 前端知识体系:JavaScript基础-原型和原型链-理解原型设计模式以及 JavaScript中的原型规则

    理解原型设计模式以及 JavaScript中的原型规则(原文地址) 1.原型对象:我们创建的每一个函数(JavaScript中函数也是一个对象)都有一个原型属性 prototype,原型属性实质上是一 ...

  4. Navicat Premium 12 安装 与 激活

    官方简体中文下载网址:https://www.navicat.com.cn/download/navicat-premium 安装的过程....(都是中文)所以略过. 开始破解....( ...... ...

  5. hivesql中的concat函数,concat_ws函数,concat_group函数之间的区别

    一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +---- ...

  6. 题解 [BZOJ4886] 叠塔游戏

    题面 解析 这是个有趣的建图题啊. 首先我们可以发现,宽度严格递增是没什么用的. 因为实际上我们在旋转完以后, 矩形的顺序是可以随便排的. 因此只要保证宽度互不相同就行了. 然后,我们对长和宽离散化, ...

  7. 【leetcode】1290. Convert Binary Number in a Linked List to Integer

    题目如下: Given head which is a reference node to a singly-linked list. The value of each node in the li ...

  8. 百度AI接口调用

    创建应用 登录网站 登录www.ai.baidu.com 进入控制台 进入语音技术 创建应用 管理应用 技术文档 SDK开发文档 接口能力 版本更新记录 注意事项 目前本SDK的功能同REST API ...

  9. java线程之sleep

    翻译:https://www.journaldev.com/1020/thread-sleep-java 简述 Thread .sleep()方法用来暂停当前线程的执行,以毫秒为单位.还有另一个重载方 ...

  10. mysql 查询相关命令

    1. 结果集按列展示 mysql  -u用户名  -p密码  -D 数据库名 -e  sql语句 示例:mysql   -uroot  -p123456  -D mysql -e select * f ...