codeforces 1321 C. Remove Adjacent(水 • 思维)

C. Remove Adjacent

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 1≤i≤|s|1≤i≤|s| during each operation.

For the character sisi adjacent characters are si−1si−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&nbs***bsp;s5=s5= a). The string becomes s=s= acaaand 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.

 

题意:

给出一个字符串,当一个字符相邻其字典序中前一个字符时, 这个字符可以删去,比如 cbca,第一位 c 相邻 b ,可以删去变成bca,第二位 c 相邻 b ,删去,变成 ba,第一位 b 相邻 a ,删去,变成a, 所以删了 3 次。

思路:

每一步在可以删掉的字符中挑选最大的字符删

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;

string s;
int n, id;

bool slove()
{
    char maxx = 'a';
    bool flag = 0;
    for(int i = 1; i <= n; ++i)
    {
        if(s[i] != 'a' && (s[i - 1] == s[i] - 1 || s[i + 1] == s[i] - 1))
        {
            if(maxx < s[i])
            {
                flag = 1;
                maxx = s[i];
                id = i;
            }
        }
    }
    return flag;
}

int main()
{
    while(~scanf("%d", &n))
    {
        getchar();
        getline(cin, s);
        s = '#' + s + '#';
        string :: iterator k;
        int cnt = 0;
        while(slove())
        {
            k = s.begin() + id;
            s.erase(k);
            n--;
            cnt++;
        }
        cout<<cnt<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务