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 si≠sn−i+1si≠sn−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),si≠sn−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;
}
相关推荐
Irissgwe7 分钟前
c++11(lambda表达式与包装器、线程库)
c++·c++11·lambda表达式·线程库·包装器·互斥量库·条件变量库
Peter·Pan爱编程41 分钟前
14. Lambda 表达式:随手可写的函数对象
c++·算法·ai编程
不想写代码的星星1 小时前
从分支预测角度看 C++:为什么你的热循环慢得离谱?
c++
郝学胜-神的一滴2 小时前
Qt 高级开发 018:复刻经典登录界面布局与窗口美化全解析
开发语言·c++·qt·程序人生·用户界面
郝亚军2 小时前
IEEE 754 单精度浮点的SEM表示
开发语言·c++·算法
Yyyyyy~3 小时前
【C++】数组篇
开发语言·c++
qq_333120973 小时前
C++高并发内存池的整体设计和实现思路_C 语言
java·c语言·c++
牛肉在哪里3 小时前
ros2 从零开始27 编写广播C++
开发语言·c++·机器人
Curvatureflight4 小时前
前端国际化 i18n 落地实践:语言包、动态文案和格式化问题怎么处理?
前端·c++·vue
黄小白的进阶之路4 小时前
C++提高编程---3.9 STL-常用容器-map/multimap 容器【P231~P235】
c++