Don't Be a Subsequence

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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.


来源/分类

ABC071&ARC081 

GPLv2 licensed by HUSTOJ 2018


题意是让你找最短的且字典序最小的,不是S子序列的序列。

考虑从后向前分割区间,每个区间包含且只包含一套a-z的字母。设每个区间的左右端点为Li,Ri。

若分割成的完整的区间个数为cut,那么易知ans=cut+1。

考虑答案串的第一个字母,如果第一个字母在[0 ,  L0)里出现过,那么以这个字母开头的长度为cut+1的所有串都在S中出现过。

因此,答案的第一个字母是[0 ,  L0)里没出现过的字母。

考虑答案的第二个字母,设第一个字母在S中第一次出现的位置为x,那么若第二个字母取区间[x+1  , R1]里的字母,那么以这个字母开头的所有长度为cut的串都在S中出现过。再加上答案的第一个字母,因此以这两个字母开头的答案为cut+1的串都在S中出现过,因此答案的第二个字母要取[x+1  , R1]里没出现的字母。

以此类推,再贪心选择即可得到答案串。

#include<bits/stdc++.h>
#define N 250000
using namespace std;

int L[N],R[N],ans=0;
int check[30]={0};

int main()
{
    char s[N];
    scanf("%s",s);
    int len=strlen(s);
    int now=26;
    R[0]=len-1;


    for(int i=len-1;i>=0;i--)
    {
        if(!check[s[i]-'a'])
        {
            now--;
            check[s[i]-'a']=1;

            if(now==0)
            {
                L[ans]=i;
                ans++;
                R[ans]=i-1;
                now=26;
                memset(check,0,sizeof(check));
            }
        }
    }
    L[ans]=0;
    int last=0;
    char ans_now;

    for(int i=ans;i>=0;i--)
    {
        int now=26;
        memset(check,0,sizeof(check));

        for(int j=last;j<=R[i];j++)check[s[j]-'a']=1;

        for(int j=0;j<26;j++)
        if(!check[j])
        {
            printf("%c",j+'a');
            ans_now=j+'a';
            break;
        }

        for(int j=last;j<len;j++)if(s[j]==ans_now)
        {
            last=j+1;
            break;
        }

    }
return 0;
}
/*
abcdefghijklmnopqrstuvwxyz
*/
View Code
原文地址:https://www.cnblogs.com/tian-luo/p/9382462.html