Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) 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.
- During the first move, you can remove the first character s1=s1= b because s2=s2= a. Then the string becomes s=s= acabcab.
- During the second move, you can remove the fifth character s5=s5= c because s4=s4= b. Then the string becomes s=s= acabab.
- During the third move, you can remove the sixth character s6=s6='b' because s5=s5= a. Then the string becomes s=s= acaba.
- 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= 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||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.
- During the first move, remove the third character s3=s3= d, ss becomes bca.
- During the second move, remove the second character s2=s2= c, ss becomes ba.
- And during the third move, remove the first character s1=s1= b, ss becomes a.
题意:给定一个字符串,若字符串中的某个字符的前一个或者后一个是其字典序的前一个字母就可以去掉这个字符,问最多能去掉多少个字符
思路:贪心,每次去掉可以去掉字符的最大字典序的字母,可以脑补一下,如果每次都不去掉当前可以去掉的最大字典序的字母,有可能会导致策略不优,很明显嘛,比如4 bcda,你先去掉c,那么就是bda,只有1,而答案是3,这个需要多想想,其实也很显然的
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
string s,ans[105];
int main()
{
cin>>n>>s;
ll tot=1,cnt=0;
ans[tot]=s;
while(1)
{
vector<char>v;
for(rg i=0;ans[tot][i];i++)
{
if(i==0)
{
if(ans[tot][i]-ans[tot][i+1]==1)v.push_back(ans[tot][i]);
continue;
}
if(i==ans[tot].length()-1)
{
if(ans[tot][i]-ans[tot][i-1]==1)v.push_back(ans[tot][i]);
continue;
}
if((ans[tot][i]-ans[tot][i-1]==1)||(ans[tot][i]-ans[tot][i+1]==1))v.push_back(ans[tot][i]);
}
//cout<<ans[tot]<<endl;
if(!v.size())break;
else
{
sort(v.begin(),v.end());
char ansc=v[v.size()-1];
ll flag=0;
for(rg i=0;ans[tot][i];i++)
{
if(ans[tot][i]==ansc&&!flag&&((ans[tot][i]-ans[tot][i-1]==1)||(ans[tot][i]-ans[tot][i+1]==1)))
{
flag=1;
continue;
}
else ans[tot+1]+=ans[tot][i];
}
}
tot++;
//cout<<ans[tot]<<endl;
cnt++;
}
cout<<cnt<<endl;
// while(1)getchar();
return 0;
}
- Spring Boot 1.5.x新特性:动态修改日志级别
- XMPP客户端库Smack 4.0.6版开发之二
- Spring Cloud实战小贴士:版本依赖关系
- 如何优雅的用Python做接口自动化测试
- 忘记oracle的sys用户密码怎么修改以及Oracle 11g 默认用户名和密码
- hibernate链接数据库链接池c3p0配置
- Oracle中session和processes的设置
- ssh相关原理学习与常见错误总结
- PyQt5 GUI应用程序工具包入门(1)
- grpc部署初体验
- Java中的ReentrantLock和synchronized两种锁机制的对比
- 用Python从零开始创建区块链
- 基于 Python 的僵尸网络将 Linux 机器变成挖矿机器人
- Oracle导入导出常用命令
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法