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;
}
相关推荐
用户8055336980311 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK1 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境1 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境1 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴2 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境4 天前
C++ 的Eigen 库全解析
c++
卷无止境4 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴4 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18006 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴6 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake