E - Don't Be a Subsequence
Time limit
配点 : 600 点
問題文
文字列 S に対して,その文字列を構成する文字を 0 文字以上取り除き,残った文字を元の順番で並べて得られる文字列を S の部分列と呼びます. たとえば,arc
や artistic
や (空文字列) は artistic
の部分列ですが,abc
や ci
は artistic
の部分列ではありません.
英小文字からなる文字列 A が与えられます. このとき,英小文字からなる文字列で,A の部分列ではないようなもののうち,最も短いものを求めてください. ただし,そのようなものが複数ある場合には,辞書順で最小のものを求めてください.
制約
- 1≤|A|≤2×105
- A は英小文字のみからなる.
入力
入力は以下の形式で標準入力から与えられる。
A
出力
英小文字からなる A の部分列でないような最短の文字列のうち,辞書順最小のものを出力せよ.
入力例 1
atcoderregularcontest
出力例 1
b
atcoderregularcontest
という文字列は a
を部分列として含みますが,b
は含みません.
入力例 2
abcdefghijklmnopqrstuvwxyz
出力例 2
aa
入力例 3
frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn
出力例 3
aca
Score : 600 points
Problem Statement
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
Input is given from Standard Input in the following format:
A
Output
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A.
Sample Input 1
atcoderregularcontest
Sample Output 1
b
The string atcoderregularcontest
contains a
as a subsequence, but not b
.
Sample Input 2
abcdefghijklmnopqrstuvwxyz
Sample Output 2
aa
Sample Input 3
frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn
Sample Output 3
aca
预处理 下标i及以后第一次出现字母j的位置。dp[i]记录从i开始到结尾的子串中题目所求的子串的最短长度。显然有dp方程 dp[i]=min{dp[nxt[i][c]+1]+1}(c∈[a,z])
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <set> 6 #include <map> 7 #include <string> 8 #include <cstring> 9 #include <stack> 10 #include <queue> 11 #include <cmath> 12 #include <ctime> 13 #include<bitset> 14 #include <utility> 15 using namespace std; 16 #define rank rankk 17 #define mp make_pair 18 #define pb push_back 19 #define xo(a,b) ((b)&1?(a):0) 20 //#define LL ll 21 typedef unsigned long long ull; 22 typedef pair<int,int> pii; 23 typedef long long ll; 24 const int INF=0x3f3f3f3f; 25 const ll INFF=0x3f3f3f3f3f3f3f3fll; 26 //const ll M=1e9+7; 27 //const ll maxn=2e5+7; 28 //const int MAXN=1005; 29 const int MAX=2e5+5; 30 const int MAX_N=MAX; 31 //const int N=55; 32 const ll MOD=1000000007; 33 //const double eps=0.00000001; 34 int gcd(int a,int b){return b?gcd(b,a%b):a;} 35 template<typename T>inline T abs(T a) {return a>0?a:-a;} 36 template<class T> inline 37 void read(T& num) { 38 bool start=false,neg=false; 39 char c; 40 num=0; 41 while((c=getchar())!=EOF) { 42 if(c=='-') start=neg=true; 43 else if(c>='0' && c<='9') { 44 start=true; 45 num=num*10+c-'0'; 46 } else if(start) break; 47 } 48 if(neg) num=-num; 49 } 50 inline ll powMM(ll a,ll b,ll M){ 51 ll ret=1; 52 a%=M; 53 // b%=M; 54 while (b){ 55 if (b&1) ret=ret*a%M; 56 b>>=1; 57 a=a*a%M; 58 } 59 return ret; 60 } 61 void open() 62 { 63 freopen("1004.in","r",stdin); 64 freopen("out.txt","w",stdout); 65 } 66 char a[MAX]; 67 int nxt[MAX][26]; 68 int dp[MAX]; 69 int main() 70 { 71 scanf("%s",a); 72 int len=strlen(a); 73 for(int i=0;i<26;i++) 74 nxt[len][i]=len; 75 for(int i=len-1;i>=0;i--) 76 for(int j=0;j<26;j++) 77 nxt[i][j]=((a[i]==(j+'a'))?i:nxt[i+1][j]); 78 dp[len]=1; 79 for(int i=len-1;i>=0;i--) 80 { 81 dp[i]=dp[nxt[i][0]+1]+1; 82 for(int j=1;j<26;j++) 83 dp[i]=min(dp[i],dp[nxt[i][j]+1]+1); 84 } 85 int now=0; 86 while(dp[now]) 87 { 88 for(int i=0;i<26;i++) 89 if(dp[now]==dp[nxt[now][i]+1]+1) 90 { 91 printf("%c",'a'+i); 92 now=nxt[now][i]+1; 93 break; 94 } 95 } 96 printf(" "); 97 return 0; 98 }