CodeForces 1251B --- Binary Palindromes
【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的更多相关文章
- 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 ...
- Codeforces #662C Binary Table
听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...
- Codeforces 838A - Binary Blocks(二维前缀和+容斥)
838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...
- [CodeForces 663E] - Binary Table(FWT)
题目 Codeforces 题目链接 分析 大佬博客,写的很好 本蒟蒻就不赘述了,就是一个看不出来的异或卷积 精髓在于 mask对sta的影响,显然操作后的结果为mask ^ sta AC code ...
- [Codeforces]663E Binary Table
某变换好题.不过听说还有O(2^n*n^2)DP的…… Description 给定一个n*m的01矩阵,你可以选择对任意行和任意列取反,使得最终“1”的数量尽量少. Input 第一行两个整数n,m ...
- Codeforces.662C.Binary Table(状压 FWT)
题目链接 \(Description\) 给定一个\(n\times m\)的\(01\)矩阵,你可以选择一些行和一些列并将其中所有的\(01\)反转.求操作后最少剩下多少个\(1\). \(n\le ...
- CodeForces 838A Binary Blocks(前缀和)题解
题意:给你个n*m的矩阵,要求你找到一个k,k > 1,使得矩阵可以分为很多k * k的小正方形,然后进行操作把每个小正方形都变为0或1,问你怎样使操作数最小. 思路:随便暴力不可取,显然你每次 ...
- CodeForces 688B Lovely Palindromes (水题回文)
题意:给一个数n,让你找出长度为偶数,并且是第 n 个回文数. 析:你多写几个就知道了,其实就是 n,然后再加上n的逆序,不过n有点大,直接用string 好了. 代码如下: #include < ...
- CodeForces - 662C Binary Table (FWT)
题意:给一个N*M的0-1矩阵,可以进行若干次操作,每次操作将一行或一列的0和1反转,求最后能得到的最少的1的个数. 分析:本题可用FWT求解. 因为其0-1反转的特殊性且\(N\leq20\),将每 ...
随机推荐
- 使用原生js 实现点击消失效果
JQ版 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...
- Series和Dataframe分组时使用groupby函数的区别
1. Dataframe分组用groupby("列名")或者groupby(["列名1","列名2"]) import pandas as ...
- python之collections模块(nametuple,Counter)
前言: import collections print([name for name in dir(collections) if not name.startswith("_" ...
- spring-boot-configuration-processor 是干啥用的
spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 引入pom依赖 <de ...
- sql server 遍历表成一棵树结构
一棵树的层次结构都在一张表内,当有这样的需要的时候.. 可以这样玩: <!-- DepartmentDTO 对象对应 department表_查询sql --> <sql id=&q ...
- c++ const修饰词
常变量: const 类型说明符 变量名 // const int i = 20;常变量定义必需初始化赋值且变量值不可更改 常引用: const 类型说明符 &引用名 //引用:指 ...
- O(1)快速乘与O(log)快速乘
//O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MOD; return ((x*y-(LL)(((long d ...
- 【csp模拟赛3】组合数学
思路: 先排序,取最大的在剩余左边任意找k-1个数,所以是排列组合,费马小定理求逆元,预处理阶乘,注意要取模.. 代码: #include<cstdio> #include<iost ...
- 如何利用新浪官方的短网址API接口实现T.cn短链接的压缩生成
短网址的实现原理就是有一个数据表会配置文件将短网址和实际网址进行对应,当请求某个短网址时,程序跳转到对应的实际网址上去,从而实现网址的访问.目前国内最稳定最好用的是新浪T.cn短链接. 之前新浪提供了 ...
- js将正整数转化为二进制
//正整数转化为二进制 function divideBy2(decNumber) { var decStack = []; var rem; var decString = ''; while (d ...