Codeforces Round 625 (Div. 2, based on Technocup 2020 Final Round)
C
Remove Adjacent
Codeforces
1600
原题
← BD →
翻译中…

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$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. 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 \le i \le |s|$ during each operation.

翻译中…

For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+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.

翻译中…
  1. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab.
  2. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab.
  3. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba.
  4. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ 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 \le |s| \le 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.

翻译中…

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.

翻译中…
  1. During the first move, remove the third character $s_3=$ d, $s$ becomes bca.
  2. During the second move, remove the second character $s_2=$ c, $s$ becomes ba.
  3. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
翻译中…

样例

Input #1
8
bacabcab
Output #1
4
InputCopy OutputCopy
Input #2
4
bcda
Output #2
3
InputCopy OutputCopy
Input #3
6
abbbbb
Output #3
5
InputCopy OutputCopy
题目信息
题号
1321C
时间限制
2 s
内存限制
256 MB
标签
暴力
构造
贪心
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。