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\),将每 ...
随机推荐
- run zabbix with docker
#!/bin/bashdocker run --name some-zabbix-server-mysql -p 10051:10051 --net=host -e DB_SERVER_HOST=&q ...
- java中的变量和数据类型
变量和javascript的变量含义一样 在Java中,变量分为两种:基本类型的变量和引用类型的变量.(javascript中同样是这样的) 基本数据类型 基本数据类型是CPU可以直接进行运算的类型. ...
- NSNull
集合中是不能放nil值的,因为nil是结尾,但是为了存放表示什么都没有的值,可以使用NSNull,它也是NSObject的一个子类. void null(){ NSNull *nl=[NSNull n ...
- Pytest学习笔记(二) 用例执行规则
在用pytest执行用例时,可以按照如下场景来执行 1.执行目录及其子目录下的所有用例 pytest filename\ 2.执行某一个py文件下的用例 pytest filename.py 3.-k ...
- 如何用20行Python代码打造一个微信群聊助手?
今天要教大家一个黑科技,20行代码实现自己定制的微信群聊助手,可以用来活跃群气氛,好多群主创建完群后,拉完一群人,之后就一片寂静,有个群聊助手,就可以帮忙活跃群里气氛,通过今天在自己的微信上有一大批好 ...
- yolo image.c
void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabe ...
- vsftp 上传550 Permission denied解决办法
在linux服务器上安装和配置了vsftp,添加用户然后现在用这个用户登录ftp成功,能下载,但是不能上传和删除,明明这个用户的主目录给了777权限了,但是始终无法上传 解决办法:是在/var/ftp ...
- 20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验二
我们一个小组都在实验前40mins提前做完但是因为队员截图发的迟没能及时提交上蓝墨云班课的任务超时 希望老师能看见将我的经验改正,谢谢老师 https://www.cnblogs.com/xiannv ...
- 状压dp,松鼠从起点出发,拿到所有坚果,然后返回起点,求最短时间。
UVA10944 松鼠从起点出发,拿到所有坚果,然后返回起点,求最短时间. #include<iostream> #include<cstdio> #include<al ...
- vue 动态渲染数据很慢或不渲染
vue 动态渲染数据很慢或不渲染 原因是因为vue检测速度很慢,因为多层循环了,在VUE 2.x的时候还能渲染出来,1.x的时候压根渲染不出来.解决方式:在动态改变数据的方法,第一行加上 this.$ ...