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\),将每 ...
随机推荐
- 使用EntityFramework6连接MySql数据库-db first方式
准备工具: VS2013.MySQL For VisualStudio 1.1.4.Connector/Net 6.8.3 程序包管理器执行命令: Install-Package EntityFram ...
- BZOJ 3932: [CQOI2015]任务查询系统 (主席树板题)
就是裸的主席树,差分之后排序插入主席树就行了. 注意主席树查询的时候叶子节点要特判,因为本身是有size的 还有要开longlong CODE #include <cctype> #inc ...
- mysql自增主键清零方法
MySQL数据库自增主键归零的几种方法 如果曾经的数据都不需要的话,可以直接清空所有数据,并将自增字段恢复从1开始计数: truncate table table_name; 1 当用户没有trunc ...
- 018_linuxC++之_抽象类的引入
(一)参考原文链接:C++多态 (二) 抽象类 在介绍抽象类之前,我们先介绍一下纯虚函数. 1.纯虚函数 在基类中仅仅给出声明,不对虚函数实现定义,而是在派生类中实现.这个虚函数称为纯虚函数.普通函数 ...
- JAVA日期格式转换---让人不得不说的故事
链接:https://my.oschina.net/xinxingegeya/blog/394821 这是给我自己参考的,大家不惜勿喷 1.举例使用 2.各种作用 3.坑(默认中文日期,加上这个就是英 ...
- 路由器配置——OSPF协议(1)
一.实验目的:用OSPF协议使全网互通 二.拓扑图 三.具体步骤配置 (1)R1路由器配置 Router>enableRouter#configure terminalEnter configu ...
- List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedList Set有两个实现类:HashSet和LinkedHashSet AbstractSet实现了Set
List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedListSet有两个实现类:HashSet和LinkedHashSetAbstractS ...
- solr系列之solr-5.5.5 window单机版jdk-1.7 tomcat8安装
一.Solr5.5.5.Tomcat8-x64.jdk-1.7-64单机部署 1.准备安装包,下载solr和tomcat的安装,直接解压即可(上篇一提供solr的下载路径) 2.在Solr5之前都还存 ...
- volatile写读的内存语义
1,当写一个volatile变量时,JMM(java内存模型)会把该线程本地内存中的所有共享变量刷新到主内存中去 2,当读取一个volatile变量时,该线程会将本地内存置为无效,线程将从主内存中读取 ...
- Alpha冲刺(6/6)
队名:new game 组长博客:戳 作业博客:戳 组员情况 鲍子涵(队长) 燃尽图 过去两天完成了哪些任务 协调了一下组内的工作 复习了一下SuffixAutomata 接下来的计划 实现更多的功能 ...