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系统中的动态链接库机制,这其中包括但不限于全局符号介入、延迟绑定以及地址无关代码等内容。
2386 141
|
搜索推荐 UED 计算机视觉
搜索引擎优化指南:SEO关键字、长尾关键字、短尾关键字以及反向链接
SEO 代表“搜索引擎优化”。它是一种数字营销策略,旨在提高网站或网页在搜索引擎未付费结果中的在线可见性。通常,网站在搜索结果页面中排名越高,或在搜索结果列表中显示的频率越高,它将从搜索引擎用户那里获得的访问者就越多。SEO 策略可以针对各种类型的搜索,例如图像搜索、本地搜索、视频搜索、学术搜索、新闻搜索和特定行业的垂直搜索引擎。它包括一系列活动,例如关键字研究和优化、链接建设和内容创建,旨在使网站更吸引搜索引擎。除了帮助提高可见性之外,SEO 还可以帮助改善网站的用户体验和可用性。
797 0
|
人工智能 算法 测试技术
贪心算法总结
贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。 贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。
351 0
贪心算法总结
|
人工智能 Python
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
326 0
Python 蓝桥杯 动态规划 2道例题+配套1道历年真题
|
1天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
7560 32
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
1天前
|
数据采集 人工智能 前端开发
让 Coding Agent 从黑盒到透明:阿里云 Agent 观测审计数据采集实践
AI Agent 规模化落地带来执行黑盒、行为难追溯、成本难度量三大难题。阿里云基于 OTel 标准,面向 Coding Agent、个人通用助理和框架型 Agent,推出 LoongSuite Pilot、插件及探针等无侵入采集方案,让 Agent 实现可看见、可分析、可审计、可治理。
644 144
|
1天前
|
人工智能 缓存 自然语言处理
阿里Qwen3.7-Max评测:Agent能力显著提升,耗时与调用成本大幅下降
阿里云百炼推出面向智能体的旗舰大模型Qwen3.7-Max,具备长周期自主执行能力,显著提升编程、办公自动化等复杂任务处理水平;支持MCP集成与多框架兼容,并以限时5折+100万Tokens免费试用大幅降低使用门槛,助力企业高效落地AI应用。在阿里云百炼平台快速体验:https://t.aliyun.com/U/fPVHqY
|
1天前
|
人工智能 安全 定位技术
CodeGraph深度解析 让Claude Code工具调用直降七成的核心原理与实操教程
如今以Claude Code为代表的AI编程智能体已经成为开发者日常编码、项目重构、漏洞修复的必备工具。但在长期使用过程中,几乎所有开发者都会遇到同一个明显痛点:AI虽然具备强大的代码生成与分析能力,却常常陷入盲目探索的循环中。
1265 2
|
1天前
|
人工智能 弹性计算 运维
阿里云发布堡垒机智能运维Agent,运维交互进入自然语言新时代
支持自然语言运维,提升效率与安全双保障。
1172 1