D. a-Good String(分治

D. a-Good String
题意:
定义一个串为 c − 好 串 c-好串 c
∣ s ∣ = 1 , s = c |s|=1,s=c s=1,s=c
∣ s ∣ > 1 |s|>1 s>1 ,左半部分全为 c c c,右半部分为 ( c + 1 ) 好 串 (c+1)好串 (c+1),或者左半部分为 ( c + 1 ) 好 串 ) (c+1)好串) (c+1)),右半部分为 c − 好 串 c-好串 c
思路:
分治
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
string s;

int dfs(int l, int r, char c){
	if(l == r) return s[l] != c;
	int mid = (l + r) >> 1;
	int x = 0, y = 0;
	for(int i = l; i <= mid; ++i) x += s[i] != c;
	for(int i = mid + 1; i <= r; ++i) y += s[i] != c;
	x += dfs(mid + 1, r, c + 1);
	y += dfs(l, mid, c + 1);
	return min(x, y);
}
void work()
{
	cin >> n;
	cin >> s;s="@"+s;
	cout << dfs(1, n, 'a') << endl;			
}

int main()
{
	ios::sync_with_stdio(0);
	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值