Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)C. Remove Adjacent

本文探讨了一个字符串操作问题,目标是在遵循特定规则的情况下,通过移除某些字符来最大化字符串的缩减长度。详细介绍了问题背景,提供了算法思路及实现代码,强调了通过逐字符检查并移除符合条件的字符来达到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Remove Adjacent
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
8
bacabcab
outputCopy
4
inputCopy
4
bcda
outputCopy
3
inputCopy
6
abbbbb
outputCopy
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.

题意:对于i这个位置 如果有a[i-1]+1=a[i] || a[i+1]+1=a[i]
那么就可以移除a[i]这个字符,中间的空位旁边的字符移过来补上,问最多移除多少个

思路:既然是删除较大的字符,那么我们枚举字符种类也就是从字符y到字符a
对于每种字符,我们判断当前位置是不是该字符,然后向两边扫一下 删除比该字符大1&&相邻的字符即可 就是模拟 注意下删除了第i+1位后 那么第i位和第i+2位就是相邻的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
int main()
{
    int len;string s;
    cin>>len>>s;
    bool v[105]= {0};
    int ans=0;
    for(int c='y'; c>='a'; c--){

        for(int i=0; i<len; i++){
            if(s[i]==c){

                int flag=0;
                int k=0;
                for(int j=i+1; j<len; j++){
                    if(!v[j]){
                        k++;
                        if(s[j]==c+1&&k==1){
                            flag=1;
                            v[j]=1;
                            k=0;
                            ans++;
                        }
                    }
                }
                k=0;
                for(int j=i-1; j>=0; j--){
                    if(!v[j]){
                        k++;
                        if(s[j]==c+1&&k==1){
                            flag=1;
                            v[j]=1;
                            k=0;
                            ans++;
                        }
                    }
                }

            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值