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

题解:倒着跑一遍原字符串(以下编号为$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. 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...

  2. tp3.2 excel导出

    //导出操作 function exportExcel($expTitle,$expCellName,$expTableData,$names,$width){ $xlsTitle = iconv(' ...

  3. 搞定 mybatis generator 三步走

    基于idea 编辑器下maven项目使用mybatis generator快速生成持久层 添加插件:插件网址:http://www.mybatis.org/generator/running/runn ...

  4. Python--基础2

    class Ball: #def setname(self,name): def __init__(self,name): self.name = name def __kick(self): #__ ...

  5. C语言字符篇(一)字符串转换函数

      #include <stdlib.h>   double atof(const char *nptr);  将字符串转换成双精度浮点数 int atoi(const char *npt ...

  6. 7.Mongodb安全性流程

    1.安全性流程 2.超级管理员 为了更安全的访问mongodb,需要访问者提供用户名和密码,于是需要在mongodb中创建用户 采用了角色-用户-数据库的安全管理方式 常用系统角色如下: root:只 ...

  7. C17K:Lying Island

    链接 题意: 有n个人,每个人可能会说: 第x个人是好人/坏人 如果第x个人是好人/坏人,则第y个人是好人/坏人 思路: 状压dp,首先每个人所说的人只能是他前面10个人,所以对于第i个人记录下,他前 ...

  8. jQuery的Ajax初识

    1. 什么是Ajax? Ajax是“Asynchronous Javascript And XML(异步Javascript和XML)”的缩写, 是指一种创建交互式网页应用的网站开发技术. Ajax不 ...

  9. Java学习笔记17---成员方法的重载与重写

    重载是指,一个类中定义了一个成员方法后,通过修改参数个数.参数类型或参数顺序,重新实现该方法,则这两个方法互为对方的重载方法. 重写是指,子类重新实现父类的成员方法. 重载后的方法,与原方法相比: ( ...

  10. 不想停机?快使用Windows Server 2016滚动升级

    [TechTarget中国原创] Windows Server环境中的关键任务工作量总是密集的.工作量会主要集中在Hyper-V部署集群.硬件故障转移集群或者外来集群的顶部.集群中常用的表格都是基于F ...