Codeforces 1385D - a-Good String(分治 + 贪心)

D. a-Good String

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

You are given a string s[1…n] consisting of lowercase Latin letters. It is guaranteed that n=2k for some integer k≥0.

The string s[1…n] is called c-good if at least one of the following three conditions is satisfied:

The length of s is 1, and it consists of the character c (i.e. s1=c);
The length of s is greater than 1, the first half of the string consists of only the character c (i.e. s1=s2=⋯=sn2=c) and the second half of the string (i.e. the string sn2+1sn2+2…sn) is a (c+1)-good string;
The length of s is greater than 1, the second half of the string consists of only the character c (i.e. sn2+1=sn2+2=⋯=sn=c) and the first half of the string (i.e. the string s1s2…sn2) is a (c+1)-good string.
For example: “aabc” is ‘a’-good, “ffgheeee” is ‘e’-good.

In one move, you can choose one index i from 1 to n and replace si with any lowercase Latin letter (any character from ‘a’ to ‘z’).

Your task is to find the minimum number of moves required to obtain an ‘a’-good string from s (i.e. c-good string for c= ‘a’). It is guaranteed that the answer always exists.

You have to answer t independent test cases.

Another example of an ‘a’-good string is as follows. Consider the string s=“cdbbaaaa”. It is an ‘a’-good string, because:

the second half of the string (“aaaa”) consists of only the character ‘a’;
the first half of the string (“cdbb”) is ‘b’-good string, because:
the second half of the string (“bb”) consists of only the character ‘b’;
the first half of the string (“cd”) is ‘c’-good string, because:
the first half of the string (“c”) consists of only the character ‘c’;
the second half of the string (“d”) is ‘d’-good string.

Input

The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤131 072) — the length of s. It is guaranteed that n=2k for some integer k≥0. The second line of the test case contains the string s consisting of n lowercase Latin letters.

It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).

Output

For each test case, print the answer — the minimum number of moves required to obtain an ‘a’-good string from s (i.e. c-good string with c= ‘a’). It is guaranteed that the answer exists.

Example
input
6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac
output
0
7
4
5
1
1

题目大意:

给出t组样例,对于每组样例给出一个长度为偶数的字符串,要求输出得到良好字符串要修改的字符的最小数量。以字符a为例,所谓良好字符串,即字符串一分为二,其中一边全是字符a,对于另一边边再次二分,但是要求字符全是a+1 = b,再次二分,同时字符也对应的 +1,直到只剩下一个字符。

解题思路:

套娃行为,分而治之,将字符串不断二分,直到分到还剩1个字符判断要不要修改,一层一层的递归下去,左边要修改的用ans1 表示,右边需要修改的用 ans2表示,ans再加上另一边不是该字符的数量,回溯时不断去min(ans1, ans2),最后得到答案。AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int N = 2e5 + 50;
typedef long long ll;
vector<int> v;
int solve(string s, char ch)
{
	if (s.size() == 1)//剩余一个字符判断是否要修改
		return (s[0] == ch) ? 0 : 1;
	int k = s.size() / 2;
	string a = s.substr(0, k);
	string b = s.substr(k);
	int ans1 = solve(a, ch + 1);
	int ans2 = solve(b, ch + 1);
	ans1 += k - count(b.begin(), b.end(), ch);//还要加上另一边不是该字符的数量
	ans2 += k - count(a.begin(), a.end(), ch);
	return min(ans1, ans2);
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int k;
		cin >> k;
		string s;
		cin >> s;
		int ans = solve(s, 'a');
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aezakmias/article/details/107433208