ARC081E. Don't Be a Subsequence
$\newcommand{\next}{\mathsf{next}}$
Let $S$ be a string of lower case English letters. If there can be found all subsequences of length $L$ in $S$, then $S$ can be divided into $L$ segments, each contains all the 26 letters, which implies length of $S$ is at least $26L$.
This observation leads us to a solution. Let $\dp[i]$ be the maximum number of the aforementioned segments that the suffix of $S$ that starts at index $i$ can be divided into. The DP can be done in $O(|S|)$ time. The shortest string that is not a subsequence of $S$ has a length of $M = \dp[0] + 1$ ($S$ is 0-indexed).
Let $\next[i][j]$ be the position of the first occurrence of letter $j$ to the right of position $i$ (including position $i$). We can compute the $\next$ array in $O(26|S|)$ time.
Using the $\next$ and $\dp$ arrays, we can construct the answer as follows:
Start with an empty string $T$. Iterate the $\dp[0] + 1$ positions of the answer string from left to right. For each position $i$, iterate over the letters from 'a' to 'z'. For each letter $j$, check whether it is possible to get an answer if we append $j$ to $T$. Let $k$ be position of the last letter of the first occurrence of $Tj$ in $S$ as a subsequence, it is ok to append letter $j$ to $T$ if the suffix $S[k + 1, |S|)$ does not contain all subsequences of length $M - |T| - 1$ i.e. $\dp[k + 1] < M - |T| - 1$. This check can be done efficiently, see the following code for detail.
code
int main() {
string s;
scan(s);
int n = SZ(s);
vb vis(26);
int cnt = 0;
vi dp(n + 1);
int length = 0;
down (i, n - 1, 0) {
if (!vis[s[i] - 'a']) {
vis[s[i] - 'a'] = true;
++cnt;
if (cnt == 26) {
++length;
fill(all(vis), false);
cnt = 0;
}
}
dp[i] = length;
}
vv next(n, vi(26));
fill(all(next.back()), n);
next.back()[s.back() - 'a'] = n - 1;
down (i, n - 2, 0) {
rng(j, 0, 26) {
next[i][j] = s[i] - 'a' == j ? i : next[i + 1][j];
}
}
++length;
int pos = 0;
while (length > 0) {
rng (j, 0, 26) {
int t = next[pos][j];
if (t < n && dp[t + 1] == length - 1) continue;
if (t < n) {
pos = t + 1;
}
cout << char('a' + j);
break;
}
--length;
}
cout << '\n';
return 0;
}
ARC081E. Don't Be a Subsequence的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- Windows Storport Miniport 驱动开发 葵花宝典 - 翻译
Roadmap for Developing Storport Miniport Drivers Last Updated: 4/20/2017 To create a storport mini ...
- sick 激光
sick10:TiM561-2050101https://www.sick.com/cn/zh/detection-and-ranging-solutions/2d-lidar-/tim5xx/tim ...
- flask + nginx + uwsgi + ubuntu18.04部署python restful接口
目录 参考链接 效果展示 一.准备工作 1.1 可运行的python demo: 1.2 更新系统环境 二.创建python虚拟环境 三.设置flask应用程序 四.配置uWSGI 五.设置系统启动 ...
- CentOS 安装 MySQL PDO 扩展
yum install php-pdo_mysql sudo service php-fpm restart
- 表单 Flask-WTF - 使用
1 配置 可以使用Flask-WTF来处理web表单,在使用之前要先配置下,打开config.py,编辑添加如下内容 WTF_CSRF_ENABLED = True SECRET_KEY = 'you ...
- k-means和iosdata聚类算法在生活案例中的运用
引言:聚类是将数据分成类或者簇的过程,从而使同簇的对象之间具有很高的相似度,而不同的簇的对象相似度则存在差异.聚类技术是一种迭代重定位技术,在我们的生活中也得到了广泛的运用,比如:零件分组.数据评价. ...
- 第11组 Beta冲刺(1/5)
第11组 Beta冲刺(1/5) 队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11913626.html 作业博客 https://www. ...
- 使用vagrant一键部署本地php开发环境(一)
一:我们为什么需要用这玩意 我们在开发中经常会面临的问题:环境不一致,有人用Mac有人用Windos还有几个用linux的,而我们的服务器都是linux. 在我本地是可以的啊,我测了都,没有问题 ...
- git提交异常 fatal: LF would be replaced by..
git提交代码时,一直报出“fatal: LF would be replaced by CRLF in (文件名)”的异常,导致代码提交不到远程仓储.其实是,不同系统对换行符的解释不同导致的. 方法 ...
- 网络编程三要素之IP
用来标示我们计算机在互联网上的唯一性 每个设备在网络中的唯一标识 每台网络终端在网络中都有一个独立的地址,我们在网络中传输数据就是使用这个地址. ipconfig:查看本机IP192.168.12.4 ...