C. Remove Adjacent

Posted dealer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
Copy
8
bacabcab
output
Copy
4
input
Copy
4
bcda
output
Copy
3
input
Copy
6
abbbbb
output
Copy
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 44.

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

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

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 30005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < 0 || c > 9) { if (c == -) f = false; c = getchar(); }
    while (c >= 0 && c <= 9) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    bool fg=1;
    int ans = 0;
    for (char c = z; c >= a; c--)
        for (int i = 0; i<s.size(); i++)
            if (s[i] == c && (i>0 && s[i - 1] == c - 1 || i<s.size() - 1 && s[i + 1] == c - 1))
                s.erase(s.begin() + i), i = -1, ans++;
    cout << ans << endl;
    return 0;
}

以上是关于C. Remove Adjacent的主要内容,如果未能解决你的问题,请参考以下文章

C. Not Adjacent Matrix1000 / 构造

1047. Remove All Adjacent Duplicates In String做题报告

LF.79.Remove Adjacent Repeated Characters I

LeetCode 1209. Remove All Adjacent Duplicates in String II

LF.82.Remove Adjacent Repeated Characters IV

[LC] 82. Remove Adjacent Repeated Characters IV