Making Anti-Palindromes制作反回文

time limit per test

1 second

memory limit per test

256 megabytes

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.

每项测试的时间限制1秒

每项测试的内存限制256兆字节

给你一个由小写英文字母组成的字符串s

。在一次操作中,你可以交换字符串s

中的任意两个字符。

如果对于每个i(1≤i≤n),s[i]≠s[n−i+1]

,则长度为n的字符串s

称为反回文。例如,字符串"codeforces"、"string"是反回文,但字符串"abacaba"、"abc"、"test"不是。

确定使字符串s

成为反回文所需的最少操作数,如果不可能,则输出−1

输入

第一行包含一个整数t

(1≤t≤104

)---测试用例的数量。测试用例的描述如下。

每个测试用例由两行组成。第一行包含一个整数 n

(1≤n≤2⋅105

) --- 字符串 s

的长度。

第二行包含字符串 s

,由 n

个小写英文字母组成。

所有测试用例的 n

之和不超过 2⋅105

输出

对于每个测试用例,输出一个整数 --- 使字符串 s

成为反回文所需的最少操作数,如果不可能,则输出 −1

示例

输入复制

10

10

codeforces

3

abc

10

taarrrataa

10

dcbdbdcccc

4

wwww

12

cabbaccabaac

10

aadaaaaddc

14

aacdaaaacadcdc

6

abccba

12

dcbcaebacccd

输出复制

0

-1

1

1

-1

3

-1

2

2

2

注意

在第一个测试用例中,字符串"codeforces"已经是反回文,因此答案为 0

在第二个测试用例中,可以证明字符串"abc"无法通过执行允许的操作转换为反回文,因此答案为 −1

在第三个测试用例中,只需交换字符串"taarrrataa"的第二个和第五个字符即可,新字符串"trararataa"将是反回文,因此答案为 1

代码:

cpp 复制代码
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

long long min_operations_to_anti_palindrome(long long n, const string& s) {
    if (n % 2 == 1) return -1;

    map<char, long long> freq;
    for (char c : s) {
        freq[c]++;
    }

    for (const auto& p : freq) {
        if (p.second > n / 2) {
            return -1;
        }
    }

    map<char, long long> conflict_pairs;
    long long cnt = 0;
    for (long long i = 0; i < n / 2; i++) {
        if (s[i] == s[n - i - 1]) {
            conflict_pairs[s[i]]++;
            cnt++;
        }
    }

    if (cnt == 0) return 0;

    long long num = 0;
    for (const auto& p : conflict_pairs) {
        num = max(num, p.second);
    }

    if (2 * num <= cnt) {
        return (cnt + 1) / 2;
    }
    else {
        return num;
    }
}

int main() {
    long long t;
    cin >> t;

    vector<long long> results;
    while (t--) {
        long long n;
        string s;
        cin >> n >> s;

        results.push_back(min_operations_to_anti_palindrome(n, s));
    }

    for (auto& res : results) {
        cout << res << endl;
    }

    return 0;
}
相关推荐
『往事』&白驹过隙;15 分钟前
数据结构与算法汇总整理篇——链表与二叉树(常用特性的概念及细节处理)
数据结构·c++·学习·leetcode·链表·面试·二叉树
F永1 小时前
C语言专题之文件操作(巨详细)
c语言·c++·学习·青少年编程
迈克柯里喵2 小时前
CUDA算子手撕与面试指南
c++·算法·面试·职场和发展·性能优化·求职招聘·gpu算力
h^hh2 小时前
6 C/C++输⼊输出(下)(未完续)
c++
chengf2232 小时前
visual studio code配置windows c++运行调试环境
c++·windows·vscode
是阿建吖!2 小时前
【优选算法】字符串
c++·算法
杨德杰2 小时前
QT图像处理:QImage与QPixmap
c++·图像处理·qt·qimage
却道天凉_好个秋3 小时前
c++ CMakeLists.txt详解
c++·makefile·cmakelists.txt
好好学习++4 小时前
【HF设计模式】03-装饰者模式
java·c++·设计模式·装饰器模式