CF1251B.Binary Palindromes

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A palindrome is a string tt which reads the same backward as forward (formally, t[i]=t[∣t∣+1−i]t[i] = t[|t| + 1 - i] for all i∈[1,∣t∣]i \in [1, |t|]). Here ∣t∣|t| denotes the length of a string tt. For example, the strings 010, 1001 and 0 are palindromes.

You have nn binary strings s1,s2,…,sns_1, s_2, \dots, s_n (each sis_i 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,bx, a, y, b such that 1≤x,y≤n1 \le x, y \le n and 1≤a≤∣sx∣1 \le a \le |s_x| and 1≤b≤∣sy∣1 \le b \le |s_y| (where xx and yy are string indices and aa and bb are positions in strings sxs_x and sys_y respectively),
  • swap (exchange) the characters sx[a]s_x[a] and sy[b]s_y[b].

What is the maximum number of strings you can make palindromic simultaneously?

回文串是指正读和反读都相同的字符串 tt(形式化地,对所有 i∈[1,∣t∣]i \in [1, |t|],均有 t[i]=t[∣t∣+1−i]t[i] = t[|t| + 1 - i])。其中 ∣t∣|t| 表示字符串 tt 的长度。例如,字符串 010、1001 和 0 都是回文串。

你有 nn 个二进制字符串 s1,s2,…,sns_1, s_2, \dots, s_n(每个 sis_i 仅由 0 和/或 1 组成)。你可以任意次(包括零次)交换任意两个字符。被交换的字符既可以来自同一字符串,也可以来自不同字符串——没有任何限制。

形式化地,在一次操作中,你需要:

  • 选择四个整数 x,a,y,bx, a, y, b,满足 1≤x,y≤n1 \le x, y \le n 且 1≤a≤∣sx∣1 \le a \le |s_x| 且 1≤b≤∣sy∣1 \le b \le |s_y|(其中 xx 和 yy 是字符串下标,aa 和 bb 分别是字符串 sxs_x 和 sys_y 中的位置),
  • 交换字符 sx[a]s_x[a] 和 sy[b]s_y[b]。

你最多能同时使多少个字符串成为回文串?

输入格式

The first line contains single integer QQ (1≤Q≤501 \le Q \le 50) — the number of test cases.

The first line on each test case contains single integer nn (1≤n≤501 \le n \le 50) — the number of binary strings you have.

Next nn lines contains binary strings s1,s2,…,sns_1, s_2, \dots, s_n — one per line. It's guaranteed that 1≤∣si∣≤501 \le |s_i| \le 50 and all strings constist of zeroes and/or ones.

第一行包含一个整数 QQ(1≤Q≤501 \le Q \le 50),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤501 \le n \le 50),表示你拥有的二进制字符串的数量。

接下来的 nn 行每行包含一个二进制字符串 s1,s2,…,sns_1, s_2, \dots, s_n。保证对每个 ii,有 1≤∣si∣≤501 \le |s_i| \le 50,且所有字符串仅由字符 0 和/或 1 组成。

输出格式

Print QQ integers — one per test case. The ii-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the ii-th test case.

输出 QQ 个整数——每个测试用例输出一个整数。其中第 ii 个整数应为:在第 ii 个测试用例中,对给定字符串执行零次或多次交换操作后,所能同时得到的回文串的最大数量。

输入输出样例

  • 输入#1

    4
    1
    0
    3
    1110
    100110
    010101
    2
    11111
    000001
    2
    001
    11100111

    输出#1

    1
    2
    2
    2

说明/提示

In the first test case, s1s_1 is palindrome, so the answer is 11.

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=0110s_1 = \text{0110}, s2=111111s_2 = \text{111111} and s3=010000s_3 = \text{010000}.

In the third test case we can make both strings palindromic. For example, s1=11011s_1 = \text{11011} and s2=100001s_2 = \text{100001}.

In the last test case s2s_2 is palindrome and you can make s1s_1 palindrome, for example, by swapping s1[2]s_1[2] and s1[3]s_1[3].

在第一个测试用例中,s1s_1 是回文串,因此答案为 11。

在第二个测试用例中,你无法同时使全部三个字符串都成为回文串,但可以使其中任意两个字符串成为回文串。例如,令 s1=0110s_1 = \text{0110}、s2=111111s_2 = \text{111111}、s3=010000s_3 = \text{010000}。

在第三个测试用例中,我们可以使两个字符串都成为回文串。例如,s1=11011s_1 = \text{11011}、s2=100001s_2 = \text{100001}。

在最后一个测试用例中,s2s_2 已是回文串,且你可以通过交换 s1[2]s_1[2] 与 s1[3]s_1[3] 等方式使 s1s_1 成为回文串。

输入解题思路,AI测评打分。不知道怎么写?

首页