CF2267A.Turn Into a Palindrome

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ali has a string ss consisting of nn lowercase Latin letters. He also has a character cc, which is a lowercase Latin letter. In one coin, he can perform the following operation on the string ss:

  • First, he chooses an index 1≤i≤n1\le i\le n.
  • Then he replaces sis_i with the character cc.

Ali wants to turn the string ss into a palindrome∗^{\text{∗}}, but he does not want to spend too many coins on it. Your task — compute the minimum number of coins he has to spend to turn the string ss into a palindrome.

∗^{\text{∗}}A string tt of length mm is a palindrome if ti=tm−i+1t_i = t_{m-i+1} holds for every 1≤i≤m1\le i\le m

阿里有一个由 nn 个小写拉丁字母组成的字符串 ss,以及一个字符 cc(也是一个小写拉丁字母)。他每次花费一枚硬币,可以对字符串 ss 执行如下操作:

  • 首先,他选择一个下标 1≤i≤n1\le i\le n;
  • 然后,他将 sis_i 替换为字符 cc。

阿里希望将字符串 ss 变成一个回文串∗^{\text{∗}},但又不想花费过多的硬币。你的任务是:计算出将字符串 ss 变为回文串所需的最少硬币数。

∗^{\text{∗}} 一个长度为 mm 的字符串 tt 是回文串,当且仅当对每个 1≤i≤m1\le i\le m,均有 ti=tm−i+1t_i = t_{m-i+1} 成立。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains an integer nn and a lowercase Latin letter cc (1≤n≤1001\le n\le 100) — the length of the string ss and the character cc.

The second line of each test case contains the string ss consisting of nn lowercase Latin letters.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn 和一个小写拉丁字母 cc(1≤n≤1001\le n\le 100)——分别表示字符串 ss 的长度和字符 cc。

每个测试用例的第二行包含字符串 ss,该字符串由 nn 个小写拉丁字母组成。

输出格式

For each test case, output one number — the minimum number of coins Ali needs to spend for the string to become a palindrome.

对于每个测试用例,输出一个数字——Ali 为使该字符串变为回文串所需花费的最少硬币数。

输入输出样例

  • 输入#1

    5
    4 b
    abca
    3 p
    xyx
    5 e
    abcbb
    8 d
    adbccbad
    10 c
    codeforces

    输出#1

    1
    0
    2
    2
    8

说明/提示

In the first test case, in one coin, you can replace s3s_3 with b\texttt{b}. After the replacement, the string becomes abba\texttt{abba}, which is already a palindrome. It can be proven that 11 is the minimum number of coins required.

In the second test case, the string ss is already a palindrome.

In the third test case, it is enough to change s1s_1 and s5s_5 to e\texttt{e}. After two replacements, the string becomes ebcbe\texttt{ebcbe}, which is already a palindrome.

In the fourth test case, in two coins, you can replace s1s_1 and s7s_7.

在第一个测试用例中,花费一枚硬币即可将 s3s_3 替换为 b\texttt{b}。替换后,字符串变为 abba\texttt{abba},它本身已是一个回文串。可以证明,所需的最少硬币数量为 11。

在第二个测试用例中,字符串 ss 本身已是一个回文串。

在第三个测试用例中,只需将 s1s_1 和 s5s_5 均改为 e\texttt{e}。经过两次替换后,字符串变为 ebcbe\texttt{ebcbe},它本身已是一个回文串。

在第四个测试用例中,花费两枚硬币即可替换 s1s_1 和 s7s_7。

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

首页