E. Making Anti-Palindromes

文章描述了一种字符串操作,允许交换字符串中的任意两个字符。目标是确定使字符串变成反回文(即不回文)所需的最小操作数。题目给出了多个测试用例,包括不同长度和内容的字符串,并提供了若干示例解答。在某些情况下,可能无法通过操作将字符串变为反回文。
摘要由CSDN通过智能技术生成

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;
	}
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值