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;
}
相关文章
|
存储 监控 API
1688商品评论数据接口实战指南:挖掘电商洞察
要获取1688商品评论数据,先注册1688开放平台并登录,然后用Python等工具调用API获取评论信息,如内容、评分等,并存储或分析这些数据。使用时须遵守平台规定,保障数据安全及隐私,利用接口进行舆情监控、提升品牌形象,并留意接口更新以优化业务流程。
|
3天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
13天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
509 203
|
5天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
730 157
|
11天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。
|
5天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。