问题 F: Don't Be a Subsequence

时间限制: 1 Sec  内存限制: 128 MB
提交: 33  解决: 2
[提交] [状态] [讨论版] [命题人:]

题目描述

A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, arc, artistic and (an empty string) are all subsequences of artistic; abc and ci are not.
You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.

Constraints
1≤|A|≤2×105
A consists of lowercase English letters.

输入

Input is given from Standard Input in the following format:
A

输出

Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A.

样例输入

atcoderregularcontest

样例输出

b

提示

The string atcoderregularcontest contains a as a subsequence, but not b.

分析:这题。。勉强算个图论吧

  • 首先,动态规划确定最短子串的长度,dp[i]为[i….)这一段中最短的非子序列长度。
  • 则dp[i]=min(dp[i],dp[next[i][j]+1]+1)
  • 再反着走回来枚举26个字母,取字典序最小的字母输出。
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(auto i=a;i<=b;++i)
#define LL long long
#define itrange(i,a,b) for(auto i=a;i!=b;++i)
#define rerange(i,a,b) for(auto i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int dp[int(2e5+5)],alpha[30],NEXT[int(2e5+5)][30],len;
string word;
void init(){
cin>>word;
len=int(word.size());
range(i,0,25)alpha[i]=len;
rerange(i,len-1,0){
alpha[word[i]-'a']=i;
range(j,0,25)NEXT[i][j]=alpha[j];
dp[i]=int(1e9+7);
}
dp[len]=1;
}
void solve(){
rerange(i,len-1,0)
range(j,0,25)dp[i]=min(dp[i],dp[NEXT[i][j]+1]+1);
int pos=0;
rerange(i,dp[0],0)range(j,0,25)if(dp[pos]==dp[NEXT[pos][j]+1]+1){
putchar('a'+j);
pos=NEXT[pos][j]+1;
break;
}
}
int main() {
init();
solve();
return 0;
}

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 ...

  10. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

随机推荐

  1. C&C++——extern

    1.C 调用C++的函数或变量 C 调用C++的函数或变量,在C++的头文件声明为extern "C" ,C调用的时候只使用extern 声明. 可见,extern "C ...

  2. Elasticsearch 5.2.1Cluster 搭建

    1.安装java cd ~ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fw ...

  3. jw player笔记二----修改logo

    一.修改HTML5模式下的logo 见http://blog.csdn.net/xiong_mao_1/article/details/17222757 二.修改FLASH模式下的logo IE7/8 ...

  4. java字符串 64位编码

    byte[] encodeBase64 = Base64.encodeBase64("到了是是是是".getBytes("UTF-8")); System.ou ...

  5. jsp小知识点(2)

    一:自定义的函数库:在wh.tld中 <description>JSTL 1.1 functions library</description> <display-nam ...

  6. POJ3682 King Arthur's Birthday Celebration

    King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. Th ...

  7. COGS2085 Asm.Def的一秒

    时间限制:1 s   内存限制:256 MB [题目描述] “你们搞的这个导弹啊,excited!” Asm.Def通过数据链发送了算出的疑似目标位置,几分钟后,成群结队的巡航导弹从“无蛤”号头顶掠过 ...

  8. TensorFlow_曲线拟合

    # coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import os ...

  9. 意想不到的chrome调试功能

    没想到chrome的功能如此强大,绝非仅能使用console.log而已,碰到如此好文必定收藏,感谢大神,本文原创地址为:http://www.cnblogs.com/Wayou/p/chrome-c ...

  10. Map、Set、List初始化大小的影响

    import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Lis ...