codeforces #625 div.2 C. Remove Adjacent

C. Remove Adjacent

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.

思路:此题的大意为:字符串中某一字符的前一位或者后一位的字符在字典序中比它小一,那么就可以删除这个字符,问最多能删除多少个字符。
要删最多,因为只有在相邻位置存在比它小1的时候才能删除,那么就要尽可能的不去破坏小的字符,因此要从最大开始删除。我们可以从z到b依次删除。(ps:刚开始一直wa,是因为没考虑到若某位置无法删除但是它旁边的可以删除,有可能会导致它在下一步无法删除,例如bbbba,前三个b无法删除,但是第四个b删除时会导致第三个b可以删除,因此在遍历一个字母时如果有删除动作,那么需要再次遍历一次这个字符。当然不会出现一次删除操作后使得上一个字符又有了可删除的可能。)

#include <bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   cin>>n;
   string s;
   cin>>s;
   int sum=0;
   bool vst[205];
   memset(vst,true,sizeof(vst));
   for(int j=25;j>0;j--)//因为a是不可能删除的,所以从z到b即可
   {
       bool ok=false;//判断是否有删除操作
	   for(int i=0;i<n;i++)
	   {
	       if(vst[i]&&s[i]-'a'==j)
	       {
	           int ii=i-1;
	           while(ii>=0)
	           {
	               if(vst[ii])  break;
	               ii--;
	           }
	           if(ii>=0&&vst[ii])
	                if(s[ii]==s[i]-1)  {sum++;vst[i]=false;ok=true;continue;}
	            ii=i+1;
	            while(ii<n)
	            {
	                if(vst[ii]) break;
	                ii++;
	            }
	            if(ii<n&&vst[ii])
	                if(s[ii]==s[i]-1)  {sum++;vst[i]=false;ok=true;}
	       }
	   }
   if(ok)   j++;
   }
   cout<<sum;
}

再类似的题目就需要细心考虑情况了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值