C. Remove Adjacent

本文详细解析了Codeforces竞赛中的一道题目,该题目要求选手找到字符串中可删除的最大字符数量,条件是至少一个相邻字符是当前字符的前一个字母。文章通过示例解释了操作流程,并提供了一个算法解决方案。

链接: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;
}


 

public override void HandleModuleCleanEnd(object sender, CoordEventArgs arg) { string module = (string)arg.Info; // assert! lock (this.mRemovedModules) { if (!this.mRemovedModules.ContainsKey(module)) { return; // NOT removed before } if (this.mRemovedModules[module] != ModuleRemovedType.Clean) { Coordinator.GetIns().GetLogger().ErrorFormat("Job[{0}]::HandleModuleCleanEnd(): Skip module '{1}' for its remove type is '{2}'.", this.Name, module, this.mRemovedModules[module]); return; } this.mRemovedModules.Remove(module); // assert remove successfully <> } #if false if (this.mState == TaskState.Running) { // only Running do the followings // <><> yangy 20160912: set schedule mode back to 'Free' if necessary // if (!ModuleManager.GetIns().Modules.ContainsKey(module)) { return; // impossible } List<string> schRobots = NMC.Scheduling.ToolTopo.GetInstance().GetAdjacentRobots(module); if (Coordinator.GetIns().DynamicSchModeEnabled && schRobots.Count == 1) { lock (NMC.Scheduling.ToolTopo.StationList) // <><> sync with PrepareAction::CheckStart() { // <><> only 1 adjacent robot in scheduler tool topo List<string> cleanModulesInDomain = new List<string>(); foreach (Module mod in ModuleManager.GetIns().Modules.Values) { if (mod.Name != module && NMC.Scheduling.ToolTopo.GetInstance().GetRobot(schRobots[0]).IsAccessible(mod.Name) && NMC.Scheduling.ToolTopo.GetInstance().GetAdjacentRobots(mod.Name).Count == 1) { // <> found another module in robot domain and it is NOT LL-like ProcessChamber pm = mod as ProcessChamber; //if (pm != null && (pm.IsCleaning || pm.IsAbout2Clean)) if (pm != null && (pm.IsCleaning || pm.IsAbout2Clean || pm.IsPendingOffline)) // <><> yangy 20170419: for UMC RTS { cleanModulesInDomain.Add(pm.Name); } } } if (cleanModulesInDomain.Count == 0) { // <><> the current input 'module' is the last clean module foreach (Module mod in ModuleManager.GetIns().Modules.Values) { if (mod is ProcessChamber && NMC.Scheduling.ToolTopo.GetInstance().GetRobot(schRobots[0]).IsAccessible(mod.Name) && NMC.Scheduling.ToolTopo.GetInstance().GetAdjacentRobots(mod.Name).Count == 1) { // <><> 'mod' is ProcessChamber in schRobots[0] domain and is NOT LL-like chamber (mod maybe input module) NMC.Scheduling.ToolTopo.GetInstance().GetStation(mod.Name).ScheduleMode = NMC.Scheduling.ScheduleMode.Free; } } } } // <> end lock (NMC.Scheduling.ToolTopo.StationList) } Notifier.GetIns().PostEvent(CoordinatorEvents.Reschedule, new CoordEventArgs(this.mID)); Coordinator.GetIns().GetLogger().WarnFormat("Job[{0}({1})]::HandleModuleCleanEnd(): Add module '{2}' back to route.", this.Name, this.ID, module); } #else if (this.ResetDynamicSchMode(module)) //动态调度模式 { Notifier.GetIns().PostEvent(CoordinatorEvents.Reschedule, new CoordEventArgs(this.mID), string.Format("Job[{0}, {1}]::HandleModuleCleanEnd({2}).", this.ID, this.Name, module)); Coordinator.GetIns().GetLogger().WarnFormat("Job[{0}({1})]::HandleModuleCleanEnd(): Add module '{2}' back to route.", this.Name, this.ID, module); } else // if (this.mIsHeldByCleanOnGo) // <> yangy 20181113: Clean on go CIP // <><> yangy 20200325: move this check inside 'lock (this.mCleanOnGoPMs)' { lock (this.mCleanOnGoPMs) { if (this.mIsHeldByCleanOnGo) // <> yangy 20181113: Clean on go CIP { if (this.mCleanOnGoPMs.Contains(module)) { this.mCleanOnGoPMs.Remove(module); Coordinator.GetIns().GetLogger().WarnFormat("Job[{0}({1})]::HandleModuleCleanEnd(): Remove cleaning-on-go done module '{2}' from 'mCleanOnGoModules'.", this.Name, this.ID, module); } if (this.mCleaningOnGoPMs.Contains(module)) { this.mCleaningOnGoPMs.Remove(module); Coordinator.GetIns().GetLogger().WarnFormat("Job[{0}({1})]::HandleModuleCleanEnd(): Remove cleaning-on-go done module '{2}' from 'mCleaningOnGoModules'.", this.Name, this.ID, module); } if (!this.IsNoVisitDuringCleaningOnGo()) { this.mIsHeldByCleanOnGo = false; this.mCleaningOnGoPMs.Clear(); // <> yangy 20200325: no use anymore Coordinator.GetIns().GetLogger().WarnFormat("Job[{0}({1})]::HandleModuleCleanEnd(): Job will NOT be blocked by cleaning-on-go modules, then call Execute().", this.Name, this.ID); this.Execute(); } } } } #endif }
最新发布
08-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值