Don't Be a Subsequence

Posted rhythm-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Don't Be a Subsequence相关的知识,希望对你有一定的参考价值。

问题 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;
}
View Code

 

以上是关于Don't Be a Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

英语口语练习(1This is between you and me, Don't let it out. 2Don't let your dreams be dreams,

the file couldn't be opened because you don't have permission to view it

The file * couldn't be opened because you don't have permission to view it

程序员之殇 —— (Are you afraid of me? Don't be.)灵感=神秘感

How can you catch a process that is about to be launched, if you don’t know the PID yet?

新概念英语(1-39)Don't drop it!