N - Remove Adjacent CodeForces - 1321C
题目大意:删除字符,当一个字符左边或者右边存在一个比它小“1”的字符那么就可以将这个字符删除,问最多能删除多少个字符
思路,:刚开始想的是,对于单调连续的字符,可以直接删除,比如,单点增的字符只保留前边的就行了,单调减的只保留后边的。。。然后知道不能删除为止。然后wa了一下午。。
正解是复杂度是O(26*n*n),并且用到了string 里的erase
补充:
string::erase 用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(s.begin()+c,s.begin()+c+t);删除从c开始向后查t个字符。
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
string s;
int n;
bool solve()//尝试删除一个字母
{
for(int j=;j>=;j--)
for(int i=;i<s.size();i++){
if(s[i]!='a'+j)
continue;
if(i>)
if(s[i-]=='a'+j-){
s.erase(i,);
return true;
}
if(i<s.size()-)
if(s[i+]=='a'+j-){
s.erase(i,);
return true;
}
}
return false;
}
int main()
{
scanf("%d",&n);
cin>>s;
int ans=;
while(solve())
ans++;
printf("%d\n",ans); }
N - Remove Adjacent CodeForces - 1321C的更多相关文章
- Codeforces 1321C Remove Adjacent
题意 给你一个字符串,字符\(s_i\)可以被伤处当且仅当\(s_{i-1}=s_i-1\)或\(s_{i+1}=s_i-1\).问最多能删几个字符. 解题思路 其实,有个很简单的做法就是从\(z\) ...
- Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) C. Remove Adjacent(字符串,贪心,枚举)
题意: 给你一个由小写字母组成的字符串,若串中两个相邻元素字典序中也相邻,移除较大字母,问最多能移除多少个字母. 思路: 从大到小依次枚举. Tips: 注意下标的处理. 以小消大: #include ...
- [LC] 82. Remove Adjacent Repeated Characters IV
Repeatedly remove all adjacent, repeated characters in a given string from left to right. No adjacen ...
- Linux/shell: remove adjacent similar patterns
cat > temp004AA1abcAA2AA3abcAA4abcAA5AA6 awk 'BEGIN {pre=0; str="";} { if(NR==1){ i ...
- Codeforces Round #625 (Div. 2)
Contest Info Practice Link Solved A B C D E F 4/6 O O Ø Ø O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...
- PatentTips - Fair scalable reader-writer mutual exclusion
BACKGROUND The present invention relates generally to multithreaded programming and, more specifical ...
- [Algo] 118. Array Deduplication IV
Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...
- 【Virt.Contest】CF1321(div.2)
第一次打虚拟赛. CF 传送门 T1:Contest for Robots 统计 \(r[i]=1\) 且 \(b[i]=0\) 的位数 \(t1\) 和 \(r[i]=0\) 且 \(b[i]=1\ ...
- CodeForces Round #498 Div.3 A. Adjacent Replacements
http://codeforces.com/contest/1006/problem/A Mishka got an integer array aa of length nn as a birthd ...
随机推荐
- P5663 加工零件 题解
原题链接 简要题意: 给定一个图,每次询问从 \(x\) 节点开始,\(y\) 步能不能达到 \(1\) 号节点. 算法一 这也是我本人考场算法.就是 深搜 . 因为你会发现,如果 \(x\) 用 \ ...
- 动态规划-区间dp-Palindrome Removal
2019-11-09 10:31:09 问题描述: 问题求解: n = 100,典型的O(n ^ 3)的动规问题.一般来说这种O(n ^ 3)的问题可以考虑使用区间dp来解决. 区间dp是典型的三层结 ...
- 2、Spark Core职责之初始化(1)——SparkContext
SparkContext(Spark上下文) /** * Main entry point for Spark functionality. A SparkContext represents the ...
- vue封装axios
一.安装axios npm install --save axios 二.在src下面创建文件夹api=>api.js(接口集合)+http.js(封装的请求) 三.在main.js中引用api ...
- 超越Mask-RCNN:谷歌大脑的AI,自己写了个目标检测AI
这是一只AI生出的小AI. 谷歌大脑的Quoc Le团队,用神经网络架构搜索 (NAS) ,发现了一个目标检测模型.长这样: △ 看不清请把手机横过来 它的准确率和速度都超过了大前辈Mask-RCNN ...
- Linux常用命令 - find命令基础使用(重点)
1篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 首先,先 ...
- 第 k 小的数
一.寻找两个有序数组的中位数 1.1 问题描述 给定两个大小为 m 和 n 的不同时为空的有序数组 nums1 和 nums2.找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m ...
- 模块 string 常用序列字符
自从知道string模块后再也不用abcd了 >>> string.digits #数字 '0123456789' >>> string.ascii_letters ...
- [bzoj1800]fly 飞行棋<暴力>
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1800 说实话我这几天运气不错,随便在bzoj上找题都可以找到水题,这题在代码上没有丝毫难度 ...
- 使用Dism命令对Win7镜像进行操作
在操作前,我们需要下载Win7部署工具AIK和Win7原版镜像 ★镜像迅雷链接 ed2k://|file|cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408 ...