【Codeforces】1251B. Binary Palindromes

题目描述

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

You have nn binary strings s1,s2,…,sns1,s2,…,sn (each sisi 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,bx,a,y,b such that 1≤x,y≤n1≤x,y≤n and 1≤a≤|sx|1≤a≤|sx| and 1≤b≤|sy|1≤b≤|sy| (where xx and yy are string indices and aa and bb are positions in strings sxsx and sysy respectively),
  • swap (exchange) the characters sx[a]sx[a] and sy[b]sy[b].

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

Input

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

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

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

Output

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

Example

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

Note

In the first test case, s1s1 is palindrome, so the answer is 11.

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=0110s1=0110, s2=111111s2=111111 and s3=010000s3=010000.

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

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

思路

只需要判断没有奇数长度的字符串以及1的个数和0的个数都为奇数即可。

代码

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	string s;
	int t;
	int a[5];
	cin>>t;
	while(t--){
		bool flag=false;
		memset(a,0,sizeof(a));
		int n;
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>s;
			if(s.length()%2)
			   flag=true;
			for(int j=0;j<s.length();j++){
				a[s[j]-'0']++;
			}
		} 
		if(a[0]%2&&a[1]%2&&!flag)
		   n--;
		cout<<n<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值