CF1321C Remove Adjacent(周围串删除)(贪心算法)

简介: CF1321C Remove Adjacent(周围串删除)(贪心算法)

You are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s|∣s∣ . You may perform several operations on this string.


In one operation, you can choose some index ii and remove the ii -th character of ss ( s_isi ) if at least one of its adjacent characters is the previous letter in the Latin alphabet for s_isi . For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index ii should satisfy the condition 1 \le i \le |s|1≤i≤∣s∣ during each operation.


For the character s_isi adjacent characters are s_{i-1}si−1 and s_{i+1}si+1 . The first and the last characters of ss both have only one adjacent character (unless |s| = 1∣s∣=1 ).


Consider the following example. Let s=s= bacabcab.


  1. During the first move, you can remove the first character s_1=s1= b because s_2=s2= a. Then the string becomes s=s= acabcab.
  2. During the second move, you can remove the fifth character s_5=s5= c because s_4=s4= b. Then the string becomes s=s= acabab.
  3. During the third move, you can remove the sixth character s_6=s6= 'b' because s_5=s5= a. Then the string becomes s=s= acaba.
  4. During the fourth move, the only character you can remove is s_4=s4= b, because s_3=s3= a (or s_5=s5= a). The string becomes s=s= acaa and you cannot do anything with it.


Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.


输入格式



The first line of the input contains one integer |s|∣s∣ ( 1 \le |s| \le 1001≤∣s∣≤100 ) — the length of ss .


The second line of the input contains one string ss consisting of |s|∣s∣ lowercase Latin letters.


输出格式



Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.


题意翻译



给出字符串 s , 1≤∣s∣≤100 。


定义一次变换为,选中第 i 个字符移除,字符串长度减少  1,选中字符需要满足 si−1,si+1 中至少有一个是  si 的前驱。


求最多可进行几次操作。


输入输出样例



输入

8

bacabcab


输出  

4


输入  

4

bcda


输出  

3


输入  

6

abbbbb


输出  

5


分析我们可以用string来做,我们先从z字符开始进行搜索删除,然后我们在一次一次的从新搜索,最后就是答案,具体实现看代码。

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  string s;
  cin>>n>>s;
  for(int i='z';i>'a';i--) //从z开始删 
    for(int j=0;j<s.size();j++)
      if(s[j]==i&&(j>0&&s[j-1]==i-1 || j<s.length()-1&&s[j+1]==i-1))//要注意不能越界
        s.erase(j,1),j=-1;//erase(j,n); 删除从j开始的n个字符,比如erase(0,1)就是删除第一个字符j=-1,是为了从头开始遍历。 
  cout<<n-s.size()<<endl;
}
相关文章
|
6月前
|
存储 人工智能 数据可视化
满足CNAS/CMA认证,实验室信息管理系统必备功能汇总
本资料为优检云LIMS系统合规功能指南,严格对标CNAS(ISO/IEC 17025)与CMA(RB/T 214)评审要求,涵盖审计追踪、分级权限、电子签名、数据防篡改等一票否决项,以及样品全周期、仪器直连、报告智能生成、资源要素管控、质量体系运行等核心模块,助力实验室高效通过资质认定与飞行检查。
|
存储 编译器 Linux
动态链接的魔法:Linux下动态链接库机制探讨
本文将深入探讨Linux系统中的动态链接库机制,这其中包括但不限于全局符号介入、延迟绑定以及地址无关代码等内容。
2616 141
|
Java 编译器 API
带你了解“Java新特性——模块化”
带你了解“Java新特性——模块化”
1018 11
|
NoSQL Java Redis
Redis的持久化方式有哪些?它们有什么区别?
Redis的持久化方式有哪些?它们有什么区别?
330 0
|
搜索推荐 UED 计算机视觉
搜索引擎优化指南:SEO关键字、长尾关键字、短尾关键字以及反向链接
SEO 代表“搜索引擎优化”。它是一种数字营销策略,旨在提高网站或网页在搜索引擎未付费结果中的在线可见性。通常,网站在搜索结果页面中排名越高,或在搜索结果列表中显示的频率越高,它将从搜索引擎用户那里获得的访问者就越多。SEO 策略可以针对各种类型的搜索,例如图像搜索、本地搜索、视频搜索、学术搜索、新闻搜索和特定行业的垂直搜索引擎。它包括一系列活动,例如关键字研究和优化、链接建设和内容创建,旨在使网站更吸引搜索引擎。除了帮助提高可见性之外,SEO 还可以帮助改善网站的用户体验和可用性。
906 0
|
运维 负载均衡 Kubernetes
企业级运维之云原生与Kubernetes实战课程 第三章第4讲 阿里云ACK集群控制器
控制器列表 kube-controller-manager cloud-controller-manager kube-proxy 最佳实践
企业级运维之云原生与Kubernetes实战课程 第三章第4讲 阿里云ACK集群控制器
|
人工智能 算法 测试技术
贪心算法总结
贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。 贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。
443 0
贪心算法总结
|
人工智能 Python
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
412 0
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
|
SQL 分布式计算 NoSQL
MaxCompute 2.0 The Evolution of NewSQL
At the Alibaba Cloud MaxCompute session during the 2017 Computing Conference held on October 14, Lin Wei, computing platform architect of Alibaba delivered a speech.
2586 0