Codeforces 1321C Remove Adjacent 【贪心】


Codeforces 1321C


Remove Adjacent

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

In one operation, you can choose some index i and remove the i-th character of s (si) if at least one of its adjacent characters is the previous letter in the Latin alphabet for si. 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 i should satisfy the condition 1≤i≤|s| during each operation.

For the character si adjacent characters are si−1 and si+1. The first and the last characters of s both have only one adjacent character (unless |s|=1).

Consider the following example. Let s= bacabcab.

During the first move, you can remove the first character s1= b because s2= a. Then the string becomes s= acabcab.
During the second move, you can remove the fifth character s5= c because s4= b. Then the string becomes s= acabab.
During the third move, you can remove the sixth character s6=‘b’ because s5= a. Then the string becomes s= acaba.
During the fourth move, the only character you can remove is s4= b, because s3= a (or s5= a). The string becomes 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.

Input
The first line of the input contains one integer |s| (1≤|s|≤100) — the length of s.

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

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

Examples
input

8
bacabcab

output

4

input

4
bcda

output

3

input

6
abbbbb

output

5

Note
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is 4.

In the second example, you can remove all but one character of s. The only possible answer follows.

During the first move, remove the third character s3= d, s becomes bca.
During the second move, remove the second character s2= c, s becomes ba.
And during the third move, remove the first character s1= b, s becomes a.



题目大意:
给你一个字符串 s ,你可以删除一个字符,当且仅当这个字符的左右相邻两边至少一个字符是它前面一个字符(如 :abd ,那么b可以删去)。问你最多可以删去几个字符。


解题思路:
问你至多删几个,那么只要每次删除的都是当前字符串可以删除的最大的那个字符就行了,因为删最大的不会影响到其他字符(最大的就说明他不在任何其他字符前)。每次删除后更新字符串即可。




AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int n,mx;
char s[110];
int a[110];
int findmax()
{
  int maxn=0,flag=0;
  for(int i=1;i<=n;i++)
  {
    if(a[i]!='a'&&(a[i]-a[i-1]==1||a[i]-a[i+1]==1))
    {
      if(a[i]>maxn)
      {
        flag=1;
        maxn=a[i];
        mx=i;
      }
    }
  }
  return flag;
}

int main()
{
  scanf("%d",&n);
  scanf("%s",s);
  for(int i=1;i<=n;i++)//字符串数字化
  {
    a[i]=s[i-1]-'a';
  }
  a[0]=100;a[n+1]=100;//不能是0,是0有可能出错
  int cnt=0;//记录删除次数
  while(findmax())
  {
    for(int i=mx;i<=n;i++)//删除最大字符
      a[i]=a[i+1];
    n--;
    cnt++;
  }
  printf("%d\n",cnt);
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值