Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) C. 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 (sisi ) if at least one of its adjacent characters is the previous letter in the Latin alphabet for sisi . 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 1i|s|1≤i≤|s| during each operation.

For the character sisi adjacent characters are si1si−1 and si+1si+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 s1=s1= b because s2=s2= a. Then the string becomes s=s= acabcab.
  2. During the second move, you can remove the fifth character s5=s5= c because s4=s4= b. Then the string becomes s=s= acabab.
  3. During the third move, you can remove the sixth character s6=s6= 'b' because s5=s5= a. Then the string becomes s=s= acaba.
  4. During the fourth move, the only character you can remove is s4=s4= b, because s3=s3= a (or s5=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.

Input

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

The second line of the input contains one string ss consisting of |s||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 
to the effect that if the characters on either side of the current character string smaller than about 1, then you can delete the current character, and asked delete operation can have a maximum number of times. It is contemplated that the situation deleted after deleting a large character remaining characters will certainly not become less so greedy start with a large number of processing. Noted that n is small, it can be directly O (n ^ 3) erase process violence, vector a.
#include <bits/stdc++.h>
using namespace std;
int pre[105]={-1};
int nxt[105]={-1};
int main()
{
    int n;
    cin>>n;
    char s[105];
    scanf("%s",s);
    vector<char>v;
    int i,j,k,ans=0;
    for(i=0;i<n;i++)v.push_back(s[i]);
    for(i=25;i>=0;i--)//从z开始判断并删除 
    {
        vector<char>::iterator it = v.begin();
        while(it!=v.end())
        {    
            if(v.size()==1)break;
            if(it==v.begin())
            {
                if(*it=='a'+i&&*(it+1)=='a'+i-1)
                {
                    it=v.erase(it);
                    ANS ++ ; 
                    IT = v.begin (); // delete back to the beginning after a re-judgment 
                }
                 the else IT ++ ; 
            } 
            the else  IF (IT v.end == () - . 1 ) 
            { 
                IF (IT * == ' A ' + I && * (IT- . 1 ) == ' A ' + I- . 1 ) 
                { 
                    IT = v.erase (IT); 
                    ANS ++ ; 
                    IT =v.begin();
                }
                else it++;
            }
            else
            {
                if((*(it-1)=='a'+i-1||*(it+1)=='a'+i-1)&&*it=='a'+i)
                {
                    it=v.erase(it);
                    ans++;
                    it=v.begin();
                }
                else it++;
            } 
        } 

    } 
    Cout << years;
    return  0 ; 
}

 



Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12399254.html