B. Binary Palindromes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
一道傻逼题,马的codeforces用自己的热点连不上,一直交不了题;好不容易上的分一次掉没了;
这道题比较明显的规律就是当s长度为奇数时必然可以回文;而且可以贡献0和1给s长度为偶数的字符串进行换位;那么长度为偶数的字符串想要回文的话必然0和1出现的次数要为偶数,按这个规律模拟就行;
代码:
#include<bits/stdc++.h> #define LL long long #define pa pair<int,int> #define lson k<<1 #define rson k<<1|1 //ios::sync_with_stdio(false); using namespace std; const int N=200100; const int M=500100; const LL mod=1e9+7; int main(){ ios::sync_with_stdio(false); int t; cin>>t; while(t--){ int n; cin>>n; string s[100]; int sum=0; for(int i=1;i<=n;i++){ cin>>s[i]; } int ans=0; int c[2]; memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ int len=s[i].length(); if(len%2==1){ sum++; for(int j=0;j<len;j++){ c[s[i][j]-'0']++; } } else{ int a[2]; memset(a,0,sizeof(a)); for(int j=0;j<len;j++){ a[s[i][j]-'0']++; } if(a[0]%2){ ans++; } else{ sum++; } } } cout<<sum+(ans/2)*2+min(c[0]+c[1],ans%2)<<endl; } return 0; }
来源:51CTO
作者:2363025390
链接:https://blog.csdn.net/qq_44291254/article/details/102744654