前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforces 1385D(dfs)

codeforces 1385D(dfs)

作者头像
dejavu1zz
发布2020-10-23 15:22:42
4440
发布2020-10-23 15:22:42
举报
文章被收录于专栏:奇妙的算法世界

题目描述

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.

给定一个字符串,求把字符串变成a-good的最小次数

思路

每次都可以分成两半来搜,所以这道题可以分治然后dfs,复杂度为nlogn。

AC代码

代码语言:javascript
复制
#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=2*1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
string s;
int n;
int check(string str,char ch){
    int cnt=0;
    for(int i=0;i<str.size();i++){
        if(str[i]!=ch) cnt++;
    }
    return cnt;
}
int dfs(string str,char ch,int res){
	//如果剩下一个字母,说明已经改变完毕,结束递归
    if(str.size()==1){
        if(str[0]!=ch) return res+1;
        return res;
    }
    int len=str.size();
    string s1=str.substr(0,len/2);
    string s2=str.substr(len/2);
    int cnt1=check(s1,ch);//改变s1需要的次数
    int cnt2=check(s2,ch);//改变s2需要的次数

    return min(res+dfs(s1,ch+1,res)+cnt2,res+dfs(s2,ch+1,res)+cnt1);
    //比较先改变s1为good串和先改变s2为good串的次数,选最小
}
void solve(){
    cin>>n>>s;
    cout<<dfs(s,'a',0)<<endl;
}
int main(){
    IOS;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档