题目大意:删除字符,当一个字符左边或者右边存在一个比它小“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的更多相关文章

  1. Codeforces 1321C Remove Adjacent

    题意 给你一个字符串,字符\(s_i\)可以被伤处当且仅当\(s_{i-1}=s_i-1\)或\(s_{i+1}=s_i-1\).问最多能删几个字符. 解题思路 其实,有个很简单的做法就是从\(z\) ...

  2. Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) C. Remove Adjacent(字符串,贪心,枚举)

    题意: 给你一个由小写字母组成的字符串,若串中两个相邻元素字典序中也相邻,移除较大字母,问最多能移除多少个字母. 思路: 从大到小依次枚举. Tips: 注意下标的处理. 以小消大: #include ...

  3. [LC] 82. Remove Adjacent Repeated Characters IV

    Repeatedly remove all adjacent, repeated characters in a given string from left to right. No adjacen ...

  4. Linux/shell: remove adjacent similar patterns

    cat > temp004AA1abcAA2AA3abcAA4abcAA5AA6 awk 'BEGIN {pre=0; str="";} { if(NR==1){     i ...

  5. Codeforces Round #625 (Div. 2)

    Contest Info Practice Link Solved A B C D E F 4/6 O O Ø  Ø     O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...

  6. PatentTips - Fair scalable reader-writer mutual exclusion

    BACKGROUND The present invention relates generally to multithreaded programming and, more specifical ...

  7. [Algo] 118. Array Deduplication IV

    Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...

  8. 【Virt.Contest】CF1321(div.2)

    第一次打虚拟赛. CF 传送门 T1:Contest for Robots 统计 \(r[i]=1\) 且 \(b[i]=0\) 的位数 \(t1\) 和 \(r[i]=0\) 且 \(b[i]=1\ ...

  9. 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 ...

随机推荐

  1. python+selenium之悠悠博客学习笔记

    1 Python之自动化测试框架selenium学习 offical website 悠悠之selenium浅谈·博客园 悠悠软件测试系列 1.1 基础环境准备 1.1.1 python包下载工具的安 ...

  2. CF1324E Sleeping Schedule 题解

    原题链接 简要题意: 每次可以将 \(a_i\) 减 \(1\) 或不变.求让 \(a_i\) 的前缀和 \(\% h\) 的值在 \([l,r]\) 区间中的最多的个数. E题是个水dp,也不怎样 ...

  3. 洛谷 P5596 【XR-4】题 题解

    原题链接 本题只要 推式子 就可以了. \[y^2-x^2=ax + b \] \[a x + x^2 = y^2 - b \] \[4 x^2 + 4 ax = 4 y^2 - 4b \] \[(2 ...

  4. HDU-4252 A Famous City(单调栈)

    最后更新于2019.1.23 A Famous City ?戳这里可以前往原题 Problem Description After Mr. B arrived in Warsaw, he was sh ...

  5. OpenCV-Python 立体图像的深度图 | 五十二

    目标 在本节中, 我们将学习根据立体图像创建深度图. 基础 在上一节中,我们看到了对极约束和其他相关术语等基本概念.我们还看到,如果我们有两个场景相同的图像,则可以通过直观的方式从中获取深度信息.下面 ...

  6. iOS UmbrellaFramework

    一.umbrella framework 将几个已经封装好的 framework 封装成一个,封装的这种 framework 就是 umbrella framework. Apple 的官方文档中明确 ...

  7. .NET(C#)实现桌面背景切换(控制台应用程序,windows服务版的未实现成功)

    AdvancedBackgroundJimmy.Program.cs using AdvancedBackground; using Microsoft.Win32; using System; us ...

  8. JavaScript登录记住密码操作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  9. 汇编学习二-VB(常见函数分析)

    VB代码如下所示 push ebp 00401FF1 . 8BEC mov ebp,esp 00401FF3 . 83EC 0C sub esp,0xC push <jmp.&MSVBV ...

  10. SQL Server 创建链接服务器的脚本,自定义链路服务器的简短名称

    USE [master]GO /****** Object:  LinkedServer [SQL01]    Script Date: 2020/4/9 11:51:17 ******/EXEC m ...