ARC081E. Don't Be a Subsequence

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


posted @   Pat  阅读(195)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
阅读排行:
· 盘点!HelloGitHub 年度热门开源项目
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· C#使用yield关键字提升迭代性能与效率
· 回顾我的软件开发经历(1)
历史上的今天:
2016-11-22 Palindrome Index
2016-11-22 Two-Pointer 之 Run Length Coding (RLC)
2016-11-22 求1...n中因子最多的数
2016-11-22 hackerrank Similar Pair
2016-11-22 区间第K大(一)
2016-11-22 POJ 2942 Knights of the Round Table
2016-11-22 Problems about trees
点击右上角即可分享
微信分享提示