其他分享
首页 > 其他分享> > C. Remove Adjacent

C. Remove Adjacent

作者:互联网

链接:https://codeforces.ml/contest/1321/problem/C

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 (or 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.
#include<bits/stdc++.h>
using namespace std;
long long n,t,s;
char x[1000001];
int main()
{
	scanf("%lld\n",&n);
	cin>>x;
	s=0;
	for(int i=25;i>0;i--)
	{
		char y=i+'a',z=i-1+'a';
		for(int j=0;j<n;j++)
		{
			if(x[j]==y)
			{
				int l=j-1,r=j+1;
				while(x[l]=='?'||x[l]==y)
				{
					l--;
				}
				if(l>=0)
				{
					if(x[l]==z)
					{
						s++;
						x[j]='?';
						continue;
					}
				}
				while(x[r]=='?'||x[r]==y)
				{
					r++;
				}
				if(r<n)
				{
					if(x[r]==z)
					{
						s++;
						x[j]='?';
					}
				}
			}
		}
	}
	cout<<s<<endl;
}


 

龍木 发布了218 篇原创文章 · 获赞 22 · 访问量 2万+ 私信 关注

标签:string,ss,move,character,Remove,remove,Adjacent,first
来源: https://blog.csdn.net/Luoriliming/article/details/104606467