308834: CF1582C. Grandma Capa Knits a Scarf
Memory Limit:256 MB
Time Limit:1 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Grandma Capa Knits a Scarf
题意翻译
### 题目描述 给一个由小写字母组成的字符串 $s$ ,选择一个字母,删去这个字符串中任意个这个字母,使这个字符串变成回文的。如果存在多个答案,输出删除的字母个数最少的答案。 ### 输入格式 第一行输入一个正整数 $t$ ,表示数据组数。 对于每一组数据,第一行输入一个正整数 $n$ 表示字符串长度,第二行输入长度为 $n$ 的字符串 $s$ 。 ### 输出格式 对于每组数据输出一行一个整数,如果存在方案,输出最少删除的字母的个数,如果不存在,输出 $-1$ 。 ### 数据范围 $1\le t\le100,1\le n\le10^5,1\le\sum n\le2\times10^5$ 。 ### 样例解释 第一组数据,删除 $2$ 个 $a$ ,字符串变为 $bcaacb$ 。 第二组数据,方案不存在。 第三组数据,字符串本身就是回文的。 第四组数据,删除 $3$ 个 $r$ ,字符串变为 $palap$ 。 第五组数据,删除 $2$ 个 $y$ ,字符串变为 $khyhhyhk$ 。题目描述
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.输入输出格式
输入格式
The first line contains a single integer $ t $ ( $ 1 \le t \le 100 $ ) — the number of test cases. The next $ 2 \cdot 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 \le n \le 10^5 $ ) — 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 \cdot 10^5 $ .
输出格式
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
5
8
abcaacab
6
xyzxyz
4
abba
8
rprarlap
10
khyyhhyhky
输出样例 #1
2
-1
0
3
2