CF2267A.Turn Into a Palindrome
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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:
- First, he chooses an index 1≤i≤n.
- Then he replaces si with the character c.
Ali wants to turn the string s into a palindrome∗, 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.
∗A string t of length m is a palindrome if ti=tm−i+1 holds for every 1≤i≤m
阿里有一个由 n 个小写拉丁字母组成的字符串 s,以及一个字符 c(也是一个小写拉丁字母)。他每次花费一枚硬币,可以对字符串 s 执行如下操作:
- 首先,他选择一个下标 1≤i≤n;
- 然后,他将 si 替换为字符 c。
阿里希望将字符串 s 变成一个回文串∗,但又不想花费过多的硬币。你的任务是:计算出将字符串 s 变为回文串所需的最少硬币数。
∗ 一个长度为 m 的字符串 t 是回文串,当且仅当对每个 1≤i≤m,均有 ti=tm−i+1 成立。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of the test cases follows.
The first line of each test case contains an integer n and a lowercase Latin letter c (1≤n≤100) — the length of the string s and the character c.
The second line of each test case contains the string s consisting of n lowercase Latin letters.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤500)。随后是测试用例的描述。
每个测试用例的第一行包含一个整数 n 和一个小写拉丁字母 c(1≤n≤100)——分别表示字符串 s 的长度和字符 c。
每个测试用例的第二行包含字符串 s,该字符串由 n 个小写拉丁字母组成。
输出格式
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 s3 with b. After the replacement, the string becomes abba, which is already a palindrome. It can be proven that 1 is the minimum number of coins required.
In the second test case, the string s is already a palindrome.
In the third test case, it is enough to change s1 and s5 to e. After two replacements, the string becomes ebcbe, which is already a palindrome.
In the fourth test case, in two coins, you can replace s1 and s7.
在第一个测试用例中,花费一枚硬币即可将 s3 替换为 b。替换后,字符串变为 abba,它本身已是一个回文串。可以证明,所需的最少硬币数量为 1。
在第二个测试用例中,字符串 s 本身已是一个回文串。
在第三个测试用例中,只需将 s1 和 s5 均改为 e。经过两次替换后,字符串变为 ebcbe,它本身已是一个回文串。
在第四个测试用例中,花费两枚硬币即可替换 s1 和 s7。
输入解题思路,AI测评打分。不知道怎么写?