B. Binary Palindromes-------------思维(逆向)

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.

Example
inputCopy

4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
outputCopy
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].

题意:给n对字符串,可以任意两对交换一个字符构成回文串,请问最多有多少个回文串。

解析:对于不能构成回文串的情况只有一种(奇数个1,奇数个0).
对于长度为奇数的字符串,一定能构成。因为奇数=偶数+奇数。所以这个要特判。
所以最终我们要找长度不是奇数且有奇数个1,奇数个0的情况就行。

#include<bits/stdc++.h>
using namespace std;
int  t,n;
char s[1000005];
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int l=0,r=0,f=0;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			int m=strlen(s);
			if(m&1) f=1;
			for(int j=0;j<m;j++)
			{
				if(s[j]=='1') l++;
				else r++;
			}
		}
		if((l&1)&&(r&1)&&!f) n--;
		cout<<n<<endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值