Don't Be a Subsequence
阿新 • • 發佈:2018-07-27
tro return 時間 algorithm times event tar tchar char
提交: 33 解決: 2
[提交] [狀態] [討論版] [命題人:]
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.
A
問題 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