第 1123 场 Codeforces Round(Div. 2)
A
变成回文串
Turn Into a Palindrome
Codeforces
800
原题
←B →

变成回文串AI (CI翻译)

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

Ali 有一个由 $n$ 个小写拉丁字母组成的字符串 $s$。他还有一个字符 $c$,它也是一个小写拉丁字母。每花费一枚硬币,他可以对字符串 $s$ 执行以下操作:AI (CI翻译)

  • First, he chooses an index $1\le i\le n$.
  • Then he replaces $s_i$ with the character $c$.
  • 首先,他选择一个下标 $1\le i\le n$。
  • 然后,他将 $s_i$ 替换为字符 $c$。
AI (CI翻译)

Ali wants to turn the string $s$ 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 $s$ into a palindrome.

Ali 想要将字符串 $s$ 变成回文串$^{\text{∗}}$,但不想为此花费太多硬币。你的任务是计算他将字符串 $s$ 变成回文串所需花费的最少硬币数。AI (CI翻译)

$^{\text{∗}}$A string $t$ of length $m$ is a palindrome if $t_i = t_{m-i+1}$ holds for every $1\le i\le m$

$^{\text{∗}}$如果对于每个 $1\le i\le m$,字符串 $t$ 都满足 $t_i = t_{m-i+1}$,则称长度为 $m$ 的字符串 $t$ 是回文串。AI (CI翻译)

Input

输入

AI (CI翻译)

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

每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 500$)。接下来是各个测试用例的描述。AI (CI翻译)

The first line of each test case contains an integer $n$ and a lowercase Latin letter $c$ ($1\le n\le 100$) — the length of the string $s$ and the character $c$.

每个测试用例的第一行包含一个整数 $n$ 和一个小写拉丁字母 $c$($1\le n\le 100$),分别表示字符串 $s$ 的长度和字符 $c$。AI (CI翻译)

The second line of each test case contains the string $s$ consisting of $n$ lowercase Latin letters.

每个测试用例的第二行包含一个由 $n$ 个小写拉丁字母组成的字符串 $s$。AI (CI翻译)

Output

输出

AI (CI翻译)

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

对于每个测试用例,输出一个数字,表示 Ali 将字符串变成回文串所需花费的最少硬币数。AI (CI翻译)

Note

说明

AI (CI翻译)

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

在第一个测试用例中,花费一枚硬币将 $s_3$ 替换为 b,即可将字符串变为 abba,它已经是一个回文串。可以证明,所需的最少硬币数为 $1$。AI (CI翻译)

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

在第二个测试用例中,字符串 $s$ 已经是回文串。AI (CI翻译)

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

在第三个测试用例中,只需将 $s_1$ 和 $s_5$ 替换为 e。经过两次替换后,字符串变为 ebcbe,它已经是一个回文串。AI (CI翻译)

In the fourth test case, in two coins, you can replace $s_1$ and $s_7$.

在第四个测试用例中,可以花费两枚硬币将 $s_1$ 和 $s_7$ 进行替换。AI (CI翻译)

样例

Input #1
5
4 b
abca
3 p
xyx
5 e
abcbb
8 d
adbccbad
10 c
codeforces
Output #1
1
0
2
2
8
InputCopy OutputCopy
题目信息
题号
2267A
时间限制
1 s
内存限制
256 MB
标签
贪心
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。