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
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
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.
思路:其实我们仔细想想,对于z来说,是没有任意一个字母依靠着z来消除的,那么我们先消除z,是没有影响的。如果没有z了呢?那么y也是一样的。这样思路就来了,我们每一个遍历都消除一个可以消除的并且是最大的字母,这样的话,我们就可以保证不会有字母漏消。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e2+1;
string s;
int n,pos;
inline int check()
{
char _max='a';
int flag=0;
for(int i=1;i<=n;i++)
{
if(s[i]-s[i-1]==1||s[i]-s[i+1]==1)
{
if(_max<s[i])
{
_max=s[i];
pos=i;
flag=1;
}
}
}
return flag;
}
int main()
{
scanf("%d",&n);
cin>>s;
s='@'+s+'#';
int cnt=0;
while(check())
{
s.erase(pos,1);
n--;cnt++;
}
cout<<cnt<<endl;
return 0;
}
努力加油a啊,(o)/~