CF2276B.Replace and Compress

入门

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given a string ss of length nn consisting of the characters 0 and/or 1. You need to perform exactly one operation consisting of two steps:

  1. choose at most one character with index from 22 to n−1n-1 inclusive and replace it with the opposite one: 0 to 1 or 1 to 0. You may choose not to change any character. The first and the last characters of the string cannot be changed;
  2. replace each maximal block of consecutive equal characters with one such character.

A block is called maximal if it cannot be extended to the left or to the right by adding the same character. For example, after the second step, the string 100111011 will become 10101.

Count the number of different strings that can be obtained after performing this operation.

给你一个长度为 nn 的字符串 ss,它仅由字符 0 和/或 1 组成。你需要恰好执行一次如下两步操作:

  1. 选择至多一个下标在 22 到 n−1n-1(含端点)之间的字符,并将其翻转(即 0 变为 1,1 变为 0);你也可以选择不修改任何字符。字符串的第一个和最后一个字符不能被修改;
  2. 将字符串中每个极大连续相同字符段(即无法向左或向右扩展的连续相同字符子串)替换为单个该字符。

例如,经过第二步操作后,字符串 100111011 将变为 10101。

请计算:执行该操作后,能够得到的不同字符串的总数。

输入格式

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case consists of one line containing a string ss (3≤∣s∣≤3⋅1053 \le |s| \le 3 \cdot 10^5) consisting only of the characters 0 and 1.

Additional constraints on the input:

  • the sum of the lengths of strings ss over all test cases does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例由一行组成,该行包含一个字符串 ss(3≤∣s∣≤3⋅1053 \le |s| \le 3 \cdot 10^5),且 ss 仅由字符 0 和 1 组成。

输入的额外约束条件:

  • 所有测试用例中字符串 ss 的长度之和不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, print one integer — the number of different strings that can be obtained after performing the operation.

对于每个测试用例,输出一个整数——执行该操作后可以得到的不同字符串的数量。

输入输出样例

  • 输入#1

    7
    001
    000
    010
    00010
    100111011
    001011010
    1111

    输出#1

    1
    2
    2
    3
    3
    2
    2

说明/提示

In the first test case, you may choose not to change any character or replace the second character. Before compression, the resulting strings are 001 and 011, respectively. After compression, both strings become 01, so the answer is 11.

In the fifth test case:

  • if no character is changed, after compression we get 10101;
  • if the second character is replaced, we get 110111011, and after compression — 10101;
  • if the fifth character is replaced, we get 100101011, and after compression — 1010101;
  • if the seventh character is replaced, we get 100111111, and after compression — 101.

In total, three different strings can be obtained: 101, 10101, and 1010101.

在第一个测试用例中,你可以选择不修改任何字符,或者将第二个字符替换。压缩前,得到的字符串分别为 001 和 011;压缩后,两个字符串均变为 01,因此答案为 11。

在第五个测试用例中:

  • 若不修改任何字符,压缩后得到 10101;
  • 若替换第二个字符,则得到 110111011,压缩后为 10101;
  • 若替换第五个字符,则得到 100101011,压缩后为 1010101;
  • 若替换第七个字符,则得到 100111111,压缩后为 101。

总计可得到三个不同的字符串:101、10101 和 1010101。

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

首页