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;
}
相关文章
|
存储 编译器 Linux
动态链接的魔法:Linux下动态链接库机制探讨
本文将深入探讨Linux系统中的动态链接库机制,这其中包括但不限于全局符号介入、延迟绑定以及地址无关代码等内容。
2436 141
|
搜索推荐 UED 计算机视觉
搜索引擎优化指南:SEO关键字、长尾关键字、短尾关键字以及反向链接
SEO 代表“搜索引擎优化”。它是一种数字营销策略,旨在提高网站或网页在搜索引擎未付费结果中的在线可见性。通常,网站在搜索结果页面中排名越高,或在搜索结果列表中显示的频率越高,它将从搜索引擎用户那里获得的访问者就越多。SEO 策略可以针对各种类型的搜索,例如图像搜索、本地搜索、视频搜索、学术搜索、新闻搜索和特定行业的垂直搜索引擎。它包括一系列活动,例如关键字研究和优化、链接建设和内容创建,旨在使网站更吸引搜索引擎。除了帮助提高可见性之外,SEO 还可以帮助改善网站的用户体验和可用性。
830 0
|
人工智能 算法 测试技术
贪心算法总结
贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。 贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。
372 0
贪心算法总结
|
人工智能 Python
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
355 0
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
|
6天前
|
人工智能 JSON 安全
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
阿里云AI安全产品联动防御Fastjson攻击
1839 9
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
|
6天前
|
云安全 人工智能 安全
|
6天前
|
人工智能 自然语言处理 数据挖掘
Qwen3.8-Max-Preview深度全解析:2.4万亿参数旗舰MoE模型+Token Plan限时优惠完整落地指南
2026年7月,全新旗舰级混合专家大模型Qwen3.8-Max-Preview正式开放抢先体验,作为通义千问Qwen3系列规格最高、综合推理能力顶尖的新一代模型,该模型总参数量达到2.4万亿(2.4T),是当前线上可调用的原生多模态旗舰模型,综合推理水准对标海外顶级Fable 5模型,在复杂工程开发、长文档深度分析、多步骤智能体自治、跨境多语言创作、海量数据挖掘五大高难度业务场景实现跨越式性能提升。
837 1
|
6天前
|
人工智能 自然语言处理 数据挖掘
最新版通义千问(Qwen3.8-Max-Preview)功能介绍
2026年,通义千问正式推出全新旗舰级大模型 **Qwen3.8-Max-Preview 预览版**,作为首款突破万亿参数规格的新一代基座模型,该模型总参数量达到**2.4万亿**,采用全新迭代的MoE混合专家架构,综合推理性能、长文本处理、多模态理解、复杂任务规划能力全面超越前代Qwen3.7-Max版本,整体实力跻身全球第一梯队,可对标海外顶级旗舰模型,是当前面向复杂工程开发、多智能体协同、超长文档解析、专业办公自动化场景的最优国产基座模型。
865 0