You are given a string ss, consisting of lowercase English letters. In one operation, you are allowed to swap any two characters of the string ss.
A string ss of length nn is called an anti-palindrome, if s[i]≠s[n−i+1]s[i]≠s[n−i+1] for every ii (1≤i≤n1≤i≤n). For example, the strings "codeforces", "string" are anti-palindromes, but the strings "abacaba", "abc", "test" are not.
Determine the minimum number of operations required to make the string ss an anti-palindrome, or output −1−1, if this is not possible.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
Each test case consists of two lines. The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the string ss.
The second line contains the string ss, consisting of nn lowercase English letters.
The sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output a single integer — the minimum number of operations required to make the string ss an anti-palindrome, or −1−1 if this is not possible.
Example
input
Copy
10
10
codeforces
3
abc
10
taarrrataa
10
dcbdbdcccc
4
wwww
12
cabbaccabaac
10
aadaaaaddc
14
aacdaaaacadcdc
6
abccba
12
dcbcaebacccd
output
Copy
0 -1 1 1 -1 3 -1 2 2 2
Note
In the first test case, the string "codeforces" is already an anti-palindrome, so the answer is 00.
In the second test case, it can be shown that the string "abc" cannot be transformed into an anti-palindrome by performing the allowed operations, so the answer is −1−1.
In the third test case, it is enough to swap the second and the fifth characters of the string "taarrrataa", and the new string "trararataa" will be an anti-palindrome, so the answer is 11.
思路:
如果n为奇数因为中间的数会相等肯定不行
n为偶数时统计有多少个不同的字母
如果一个字母的个数大于n/2时也就是不管你怎么换都存在两两相等的
如果小于的话
统计多少个两两相同的字母即sum 如果两两相同那个字母就加一次
如果相同的个数中某个字母大于sum+1/2的话那么交换的个数就是要把最大的字母全都换掉
如果小于的话就只需要把sum个全部换掉
为什么要sum+1
当sum为偶数时没有影响
奇数时要多换一次
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>//to_string(value)
#include<cstdio>
#include<cmath>
#include<vector>//res.erase(unique(res.begin(), res.end()), res.end())
#include<queue>
#include<stack>
#include<map>
#include<set>//iterator,insert(),erase(),lower/upper_bound(value)/find()return end()
#define ll long long
using namespace std;
signed main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
if(n%2==1){
cout<<-1<<endl;
continue;
}
int a[26]={0};
for(int i=0;i<n;i++){
a[s[i]-'a']++;
}
int maxx=0;
for(int i=0;i<26;i++){
maxx=max(maxx,a[i]);
}
if(maxx>n/2){
cout<<-1<<endl;
continue;
}
int b[26]={0},sum=0;
for(int i=0;i<n/2;i++){
if(s[i]==s[n-i-1]){
b[s[i]-'a']++;
sum++;
}
}
maxx=0;
for(int i=0;i<26;i++){
maxx=max(maxx,b[i]);
}
cout<<max(maxx,(sum+1)/2)<<endl;
}
}