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;
}
相关文章
|
7月前
|
测试技术 索引
【每日一题Day296】LC833字符串中的查找与替换 | 排序+模拟
【每日一题Day296】LC833字符串中的查找与替换 | 排序+模拟
47 0
|
机器学习/深度学习 人工智能 算法
CF1550A Find The Array(贪心算法)
CF1550A Find The Array(贪心算法)
38 0
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制
767 0
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制
删除链表中等于给定值 val 的所有节点
删除链表中等于给定值 val 的所有节点
|
存储 算法 测试技术
LeetCode算法小抄--O(1)时间下删除-查找数组中任意元素
LeetCode算法小抄--O(1)时间下删除-查找数组中任意元素
每日一题---34. 在排序数组中查找元素的第一个和最后一个位置[力扣][Go]
每日一题---34. 在排序数组中查找元素的第一个和最后一个位置[力扣][Go]
每日一题---34. 在排序数组中查找元素的第一个和最后一个位置[力扣][Go]
AC牛客 BM4 合并两个排序的链表
AC牛客 BM4 合并两个排序的链表
98 0
AC牛客 BM4 合并两个排序的链表
|
算法 决策智能
BF算法(字符串查找)
BF算法(字符串查找)
168 0
BF算法(字符串查找)
AC牛客 BM16 删除有序链表中重复的元素-II
AC牛客 BM16 删除有序链表中重复的元素-II
71 0