题目大意:给定一个字符串,求一个最短的串要求没有在该字符串的子串中出现过,如果有多个,输出字典序最小的那一个。

题解:倒着跑一遍原字符串(以下编号为$1\sim n$),按出现了所有$26$个字母来分段,把完整的段从左到右编号,第$i$段为$[l_i,r_i]$,答案的长度就是分成的完整的段$+1$,考虑字典序最小,第一个字母一定是$[1,l_i)$中最小的没有出现的字符;对于第$i$位答案,令$x$为$ans_{i-1}$在$[l_{i-1},r_{i-1}]$中第一次出现的位置,则$ans_i$等于在$[x,r_{i-1}]$中最小的没出现过的字符

卡点:

C++ Code:

#include <cstdio>
#include <cstring>
#define maxn 200010
#define N maxn / 26 + 10
char s[maxn];
int l[N], r[N];
int cnt[26];
int sum[maxn][26], nxt[maxn][26];
int n, m, sz, ans;
char find(int r, int l = 0) {
for (int i = 0; i < 26; i++) if (!(sum[r][i] - sum[l][i])) return i + 97;
return 20040826;
}
int main() {
sz = sizeof cnt;
scanf("%s", s + 1);
n = strlen(s + 1);
int num = 0; r[m = 1] = n;
for (int i = n; i; i--) {
memcpy(nxt[i], nxt[i + 1], sz);
int x = s[i] - 'a';
nxt[i][x] = i;
num += cnt[x]++ == 0;
if (num >= 26) {
l[m] = i;
r[++m] = i - 1;
memset(cnt, 0, sz);
num = 0;
}
}
for (int i = 1; i <= n; i++) {
memcpy(sum[i], sum[i - 1], sz);
sum[i][s[i] - 'a']++;
}
putchar(ans = find(r[m]));
for (int i = m - 1; i; i--) putchar(ans = find(r[i], nxt[l[i]][ans - 'a']));
putchar(10);
return 0;
}

[AT2698] Don't Be a Subsequence的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  3. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  4. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  8. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. 写一个addEventListener以及removeEventListener

    第一步:对象属性赋值为函数,对象内部函数控制年龄这一参数变化,同时成长事件也执行. class Person{ constructor(){ this.name = ''; this.age = 0; ...

  2. 谈谈对bug的一点想法,说说做好日志记录的重要性

    说起程序猿,总绕不开的一个话题就是bug,估计每个程序猿听到某某测试跑过来一脸淫笑的告诉你这个功能有个bug的时候,总有种恨不得掐死他的想法.其实程序猿跟bug的关系,感觉有点像父亲和儿子的关系,自己 ...

  3. 利用sysbench进行MySQL OLTP基准测试

      Preface       In order to know clearly about the real performance threshold of database server,we ...

  4. tp5简单构造

    application 应用目录 网站核心index前台目录 controller 控制器admin 后台目录 model 数据模型view 视图extend 静态类库目录public 静态资源和入口 ...

  5. html_outputer.py

    coding=UTF-8 # HTML输出器 import sys class htmlOutputer(): def __init__(self): self.data = [] def colle ...

  6. 解决Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0

    解决办法是你需要将工程根目录build.gradle中的 classpath 'com.android.tools.build:gradle:2.2.0' 更改成 classpath 'com.and ...

  7. 学习网络请求返回json对应的model

    原来泛型可以这样用: 网络返回基类,返回一个code,msg,body,其中body不确定,所以,我们把它写成泛型 import org.json.JSONObject; /** * 网络请求的基类 ...

  8. Spring MVC重定向和转发

    技术交流群:233513714 转发和重定向 开始Java EE时,可能会对转发(forward)和重定向(redirect)这个两个概念不清楚.本文先通过代码实例和运行结果图片感性 认识二者的区别, ...

  9. 页面引入外部字体ttf,如何提取所需要的ttf字体或者加载过慢的解决方法-1127更新

    最近几天编写手机端的页面之后,文中需要华文行楷字体,在网上下载后,引入到了自己的前端页面,以为没有什么事了,继续码代码 @font-face { font-family:huawen; src: ur ...

  10. 2016年后web开发趋势是什么?

    2016 年后 Web开发趋势是什么 来源:yafeilee.me 发布时间:2016-05-06 阅读次数:1378 3   近二年的进展 前端发展日新月异, 甚至有一句戏言: "每六星期 ...