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;
}
相关推荐
小小龙学IT2 小时前
Boost.Interprocess 开源 C++ 进程间通信库深度解析
c++·开源
倒头就睡的小比特5 天前
算法竞赛C++常用的STL
c++·算法
weilx12345 天前
C++笔记-文件IO-<fcntl.h>
c++
Smileyqp沛沛6 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车6 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光6 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·6 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁6 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven6 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码6 天前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎