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

猜你喜欢

转载自www.cnblogs.com/Rhythm-/p/9377038.html