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

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;
}

发布了84 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43563669/article/details/104679961