Codeforces Round #625 (Div. 2) 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.

題意:
給出你一個字符串,如果一個字符的前面或者後面的字符是字典序在他前一位,那麼這個字符可以刪除,問最多能刪除多少個字符。

思路:

  1. 貪心,直接從z到a,從後往前刪。
  2. 如果貪心過程只有一次,那麼會wa10,因爲例如:
    100
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  3. 這種例子就會過不去,只要把貪心過程重複多次即可。

代碼:

#include<bits/stdc++.h>
#include<string>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define ll long long
//#define ll unsigned long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define eps 1e-6
#define PI acos(-1)
#define mysetit multiset<ll>::iterator
#define mymsetit multiset<ll>::iterator
#define mymapit map<ll,ll>::iterator
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
using namespace std;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;}
void put1(){ puts("Yes") ;}
void put2(){ puts("No") ;}
void put3(){ puts("-1"); }
ll qpf(ll a, ll b, ll p)
{ll ret = 0;while(b){if(b & 1) ret = (ret + a) % p;
a = (a + a) % p;b >>= 1;}return ret % p ;}
ll qp(ll a, ll n, ll p)
{ll ret = 1;while(n){if(n & 1) ret = qpf(ret, a, p);a = qpf(a, a, p);
n >>= 1;}return ret % p ;}
//θ=acos(L/2R);
 
const int manx=2e5+5;
 
 
int main(){
    ll n=read();
    string s;cin>>s;
    for(int i='z';i>='a';i--){
        for(int j=0;j<=100;j++){
            for(int k=0;k<s.si;k++){
                if(s[k]==i){
                    if(s[k-1]==i-1||s[k+1]==i-1)
                        s.erase(k,1),--k;
                }
            }
        }
    }
    cout<<n-s.si<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章