题目链接:点击查看

题目大意:对一个长度为 n 的字符串 s 来说,可以进行的操作如下:

  1. 选出一个二元对 ( i , i + 1 )
  2. 满足 i >= 0 && i + 1 < n
  3. s[ i ] == s[ i + 1 ]
  4. 可以将 s[ i ] 和 s[ i + 1 ] 一起删除

现在给出一个字符串,要求对其 n 个后缀分别进行操作,使得操作后的字典序最小

题目分析:首先抛开题目,只看输入输出的话,题目对于输出是有点小要求的,所以可以自己手写一个结构体来满足要求,算是一个小模拟,不多解释了

又因为是对 n 个后缀进行操作,所以我们不妨从后向前来,这样就能遍历到每个后缀了

设 ans[ i ] 是第 i 个后缀经过操作后的字符串,不难看出 ans 数组是可以递推的,因为上面说到了需要从后向前来,所以选择倒着递推,到了第 i 个位置时,分为三种情况讨论一下:

  1. 如果 s[ i ] != s[ i + 1 ],ans[ i ] =  s[ i ] + ans[ i + 1 ]
  2. 如果 s[ i ] == s[ i + 1 ]
    1. 如果删除掉 s[ i ] 和 s[ i + 1 ] 更优,那么 ans[ i ] = ans[ i + 2 ]
    2. 否则 ans[ i ] = s[ i ] + ans[ i + 1 ] = s[ i ] + s[ i ] + ans[ i + 2 ]

现在的问题转换为,讨论一下何时删除掉 s[ i ] 和 s[ i + 1 ] 是更优的,感觉这里才是这个题目的难点:

  1. s[ i ] < ans[ i + 2 ][ 0 ],显然不删是更优的
  2. s[ i ] > ans[ i + 2 ][ 0 ],显然删掉是更优的
  3. s[ i ] == ans[ i + 2 ][ 0 ],设 j 是满足 ans[ i + 2 ][ j ] != ans[ i + 2 ][ 0 ] 的最小的下标
    1. 如果 ans[ i + 2 ][ 0 ] < ans[ i + 2 ][ j ],不删是更优的
    2. 否则删掉是更优的

针对第三种情况简单举个例子,比如原字符串是 “123”,在前面加上一个 “1”,显然 “1123” < “123”,另一种情况就是如果原字符串是 “321”,在前面加上一个 “3”,就得到了 “321” < “3321”

代码:

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;struct Node
{string pref,suff;bool incr=false;int len=0;void push_front(char ch){if(!len)incr=false;else if(ch!=pref[0])incr=(ch<pref[0]);else{}len++;if(suff.size()<2)suff=ch+suff;pref=ch+pref;if(pref.size()>10)pref.pop_back();}
}ans[N];string s;int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endifios::sync_with_stdio(false);  cin>>s;for(int i=s.size()-1;i>=0;i--){char ch=s[i];if(i+1<s.size()&&s[i]==s[i+1])//可以选择删除{ans[i]=ans[i+2];if(ans[i].len!=0&&(ch<ans[i].pref[0]||ch==ans[i].pref[0]&&ans[i].incr))//不删更优 {ans[i].push_front(ch);ans[i].push_front(ch);}}else//无法选择 {ans[i]=ans[i+1];ans[i].push_front(ch);}}for(int i=0;i<s.size();i++){if(ans[i].len<=10)cout<<ans[i].len<<' '<<ans[i].pref<<endl;elsecout<<ans[i].len<<' '<<ans[i].pref.substr(0,5)<<"..."<<ans[i].suff<<endl;}return 0;
}

CodeForces - 1422E Minlexes(dp+字符串)相关推荐

  1. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

  2. CodeForces 864E Fire dp递推

    CodeForces 864E 题意:有 n 个物品着火,每个物品要花 ti 时间扑灭,且在 >= di 时间后就会坏掉,物品价值为 pi . 问最多可以救回多少价值,物品个数,及救哪些物品(要 ...

  3. Codeforces 864E - Fire(dp)

    原题连接:http://codeforces.com/problemset/problem/864/E 题意:一个人想从大火中带走一些东西.每次他只能带一个,耗时ti ,价值为pi, 当总时间超过di ...

  4. CodeForces - 1497D Genius(dp)

    题目链接:点击查看 题目大意:给出 nnn 个问题,每个问题有如下属性: tagtagtag:标签 ccc:困难度 sss:奖励值 初始时 ci=2ic_i=2^ici​=2i,初始时 IQ=0IQ= ...

  5. CodeForces - 137D Palindromes(dp+路径输出)

    题目链接:点击查看 题目大意:给出一个长度为 n 的回文串,问最小修改多少个字母,可以使得整个回文串可以被划分成不超过 k 个连续的回文串,并输出最终的划分方案 题目分析:n 很小,考虑多维 dp 首 ...

  6. CodeForces - 985F Isomorphic Strings(字符串哈希)

    题目链接:点击查看 题目大意:首先规定同构字符串,若字符串s和字符串t互为同构字符串,则必须满足: 两个字符串长度相同 s中的字符种类数与t中的字符种类数相同 s中的每一个字母在t中都有对应,且必须是 ...

  7. D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)

    D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...

  8. 动态规划 —— 线性 DP —— 字符串编辑距离

    [概述] 字符串编辑距离,即 Levenshtein 距离,是俄国科学家 Vladimir Levenshtein 提出的概念,是指从一个字符串修改到另一个字符串时,编辑单个字符所需的最少次数,编辑单 ...

  9. POJ 2817 状态DP 字符串找最多的重复

    题意: 给出字符串个数 n 给出n串字符串 找出上下两个字符串重复和最多的个数.. eg: 5 abc bcd cde aaa bfcde 0根据 aaa abc bcd cde bfcde答案就是重 ...

最新文章

  1. odoo pivot中去掉求和_一文读懂深度学习中的卷积运算与图像处理
  2. 在JPA 2.1中使用@Convert正确完成映射枚举
  3. windows运行linux脚本命令大全,查看和运行 Windows PowerShell 脚本
  4. java开发一年多少钱_Java
  5. CentOS7.4到Elasticsearch一路坑(八)(坑没填上)
  6. python configparser 空格_python的ConfigParser模块
  7. phpstom可以配置php环境吗_环境配置 · PhpStorm · 看云
  8. TouchId iOS简明教程
  9. 2021-06-28
  10. 淘宝客APP源码社交电商uniapp开发源码前端源码自营商城
  11. maya oracle 黄种子,nvidia physX 2.89 for MAYAMAX(BT种子下载)
  12. 计算机上机考试的系统,计算机上机考试系统
  13. 16种常用统计分析软件介绍
  14. VMworld大会展示最热门24款虚拟化产品
  15. uniapp 实现微信小程序全局分享及自定义分享按钮样式
  16. 2022卡塔尔世界杯引爆全球,跨境电商如何做好选品和营销?
  17. IOS Appstore 预览图尺寸
  18. 华工计算机科学与技术专业评级,华南理工高考专业分数排名,计算机分数遥遥领先,双一流专业垫底...
  19. 考研线性代数深入理解
  20. 干货 | 使用云监控实现触发一个url调用

热门文章

  1. java io复用_学习Java编程-IO复用
  2. 荧光透视的计算机辅助外科手术,「电信学」「2008.11」基于荧光透视的电磁跟踪骨科X射线导航实践研究...
  3. RabbitMQ消息应答
  4. 用户中心 - 修改用户信息
  5. 集成开发环境IDE的概述
  6. Eurek Ribbon Feign常见问题及解决
  7. 数据库分库分表的几种方式
  8. 使用Github(仓库管理)
  9. php arcode svg,在react中使用svg的各种方法总结(附代码)
  10. fedora yum mysql_Fedora14使用yum安装mysql