CF1582C.Grandma Capa Knits a Scarf
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string s of length n.
Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string s.
She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string s a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose.
A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not.
奶奶卡帕决定织一条围巾,并请爷爷谢尔为她设计一个图案,该图案是一个由小写英文字母组成的字符串。爷爷谢尔写出了一个长度为 n 的字符串 s。
奶奶卡帕想织一条漂亮的围巾,在她看来,只有回文字符串才能织出漂亮的围巾。她希望修改爷爷谢尔设计的图案,但为了不冒犯他,她将只选择一种小写英文字母,并删除字符串 s 中该字母的**若干(可选:零个、若干个或全部)**出现位置上的字符。
她还希望最小化从图案中删除的字符数量。请你帮她找出使字符串 s 变为回文串所需删除的最少字符数;若不可能实现,请告知她这是不可能的。注意:她只能删除所选定的那一种字母对应的字符。
若一个字符串从左到右读与从右到左读完全相同,则称其为回文串。例如,字符串 'kek'、'abacaba'、'r' 和 'papicipap' 是回文串,而 'abb' 和 'iq' 不是回文串。
输入格式
The first line contains a single integer t (1≤t≤100) — the number of test cases. The next 2⋅t lines contain the description of test cases. The description of each test case consists of two lines.
The first line of each test case contains a single integer n (1≤n≤105) — the length of the string.
The second line of each test case contains the string s consisting of n lowercase English letters.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤100),表示测试用例的数量。接下来的 2⋅t 行包含测试用例的描述。每个测试用例的描述由两行组成。
每个测试用例的第一行包含一个整数 n(1≤n≤105),表示字符串的长度。
每个测试用例的第二行包含字符串 s,它由 n 个小写英文字母组成。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and −1, if it is impossible.
对于每个测试用例,如果可以将字符串变为回文串,则输出使其成为回文串所需删除的最少符号数量;否则输出 −1。
输入输出样例
输入#1
5 8 abcaacab 6 xyzxyz 4 abba 8 rprarlap 10 khyyhhyhky
输出#1
2 -1 0 3 2
说明/提示
In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.
In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome.
In the third test case, you don't have to erase any symbols because the string is already a palindrome.
在第一个测试用例中,你可以选择字母 'a' 并删除其首次和末次出现的位置,得到字符串 'bcaacb',它是一个回文串。你也可以选择字母 'b' 并删除其所有出现的位置,得到字符串 'acaaca',它同样是一个回文串。
在第二个测试用例中,可以证明:无论选择哪个字母并删除其部分出现位置,都无法得到一个回文串。
在第三个测试用例中,你无需删除任何字符,因为原字符串本身就已经是回文串。
输入解题思路,AI测评打分。不知道怎么写?