CF2182A.New Year String

普及-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

We will call a string consisting of characters 0, 2, 5, and/or 6 a New Year string if at least one of the two conditions is met:

  • it contains the string 2026 as a continuous substring;
  • it does not contain the string 2025 as a continuous substring.

For example, the strings 20252026, 21026, 20262026, 000 are New Year strings. The strings 2025, 20256, 20252025, 000202500020226 are not New Year strings.

You are given a string ss. You can perform the following operation any number of times (possibly zero):

  • choose a character in the string ss and replace it with 0, 2, 5, or 6 (you can choose any one of these four characters).

Calculate the minimum number of operations needed to make the string ss a New Year string.

我们将一个仅由字符 0、2、5 和/或 6 组成的字符串称为新年字符串,当且仅当满足以下两个条件中的至少一个:

  • 它包含连续子串 2026;
  • 它不包含连续子串 2025。

例如,字符串 20252026、21026、20262026、000 都是新年字符串;而字符串 2025、20256、20252025、000202500020226 则不是新年字符串。

给定一个字符串 ss。你可以执行以下操作任意多次(包括零次):

  • 选择字符串 ss 中的一个字符,并将其替换为 0、2、5 或 6 中的任意一个(四者任选其一)。

请计算使字符串 ss 变为新年字符串所需的最少操作次数。

输入格式

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

Each test case consists of two lines:

  • the first line contains one integer nn (4≤n≤204 \le n \le 20) — the number of characters in ss;
  • the second line contains ss — a sequence of nn characters 0, 2, 5, and/or 6.

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

每个测试用例由两行组成:

  • 第一行包含一个整数 nn(4≤n≤204 \le n \le 20)—— 字符串 ss 的字符个数;
  • 第二行包含字符串 ss —— 由 nn 个字符组成的序列,每个字符为 0、2、5 和/或 6。

输出格式

For each test case, output one integer — the minimum number of operations needed to make the string ss a New Year string.

对于每个测试用例,输出一个整数——使字符串 ss 成为新年字符串所需的最少操作次数。

输入输出样例

  • 输入#1

    7
    4
    0000
    4
    2025
    4
    2026
    8
    20252026
    8
    20252025
    9
    202520256
    9
    202520265

    输出#1

    0
    1
    0
    0
    1
    1
    0

说明/提示

In the second example from the statement, you can replace the 22-nd character of the string with 2. Then the string will become 2225.

In the fifth example from the statement, you can replace the 44-th character with 6. Then the string will become 20262025.

In the sixth example from the statement, you can replace the 88-th character with 6. Then the string will become 202520266.

在题目陈述的第二个例子中,您可以将字符串的第 22 个字符替换为 2。此时字符串将变为 2225。

在题目陈述的第五个例子中,您可以将第 44 个字符替换为 6。此时字符串将变为 20262025。

在题目陈述的第六个例子中,您可以将第 88 个字符替换为 6。此时字符串将变为 202520266。

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

首页