codeforces1385D (递归+分治)
题意描述
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.
思路
根据题意描述,第一次操作时,我们可以选择 s [ 0 ] s[0] s[0]到 [ l e n / 2 ] [len/2] [len/2]来进行改变,第二次操作时,我们可以选择 s [ l e n / 2 + 1 ] s[len/2+1] s[len/2+1]到 s [ l e n ] s[len] s[len]来进行改变,之后的每次改变都会将字符串分为一半,取两次操作的最小值即可
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#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=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int n;
string s;
int check(string str,char ba){
int cnt=0;
rep(i,0,str.size()) if(str[i]!=ba) cnt++;
return cnt;
}
int dfs(string str,char ba,int res){
if(str.size()==1){
if(str[0]==ba) return res;
return res+1;
}
int len=str.size();
string s1=str.substr(0,len/2);
string s2=str.substr(len/2,len/2);
int cnt1=check(s1,ba);//对s1进行改变的结果
int cnt2=check(s2,ba);//对s2进行改变的结果
//取两次操作的最小值
return min(res+dfs(s2,ba+1,res)+cnt1,res+dfs(s1,ba+1,res)+cnt2);
}
void solve(){
cin>>n>>s;
cout<<dfs(s,'a',0)<<endl;
}
int main(){
IOS;
int t;cin>>t;
while(t--){
solve();
}
return 0;
}
- Joomla未授权创建用户漏洞(CVE-2016-8870)分析
- 如何将HDFS文件系统挂载到Linux本地文件系统
- 使用 XML 内部实体绕过 Chrome 和 IE 的 XSS 过滤器
- 响应式编程的实践
- S2-045 原理初步分析(CVE-2017-5638)
- 如何在HDFS上查看YARN历史作业运行日志
- 面向流的设计思想
- 天才第一步 Docker 纸尿裤
- 如何迁移Kudu1.2的WAL和Data目录
- WordPress REST API 内容注入漏洞分析
- 如何为Kerberos环境的CDH集群在线扩容数据节点
- WinDbg 漏洞分析调试(一)
- MySQL MVCC(多版本控制)
- 如何在集群外节点跨网段向HDFS写数据
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- centos7 下使用Supervisor监控 gin服务
- Json web token的简单实现 JAVA
- go 指针和内存分配详解
- Git如何解决本地冲突(纯净版)
- No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available:
- typescript中的class和interface
- SSH随笔
- Python爬虫+Flask,带你创建车标学习网站
- Go 堆栈的理解
- Kubernetes 与虚拟化和容器化的关系
- Golang 读、写文件
- 这次妥妥地拿下散列表---基础、如何设计以及扩展使用(LRU)
- 必看的Linux服务器高并发调优实战
- Vue3.0来了
- Golang 正则表达式(regexp)