中国石油大学OJ 第五场个人训练赛 Don't Be a Subsequence

问题 F: Don't Be a Subsequence

时间限制: 1 Sec  内存限制: 128 MB
提交: 257  解决: 53
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

提示

扫描二维码关注公众号,回复: 2594792 查看本文章

The string atcoderregularcontest contains a as a subsequence, but not b.

题意:给定一个字符串,询问不是这个字符串的子串的最小字典序的字符串。

题解:首先将字符串从后到前将每个字母插入set集合中,若set中的元素满了26个,说明在这个位置完成了一次字母的全排列,然后将set清空,设答案串为S,则S的长度一定是全排列的次数+1,从后到前,每一次全排列的开始和结束的位置视为一个区间,dp[i]为在第i个位置能够构建的S串的长度,如果一个位置pos,它后面每一个第一次出现的字母的位置的dp最小是n,则pos位置一定已经完成了第n次全排列,则它的dp[pos]=n+1,由此可以建立状态转移方程dp[i]=min(dp[i],dp[ 下一个字母第一次出现的位置 ]  +1 ),其中每个字母下一次出现的位置可以预处理得到。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
int dp[maxn],nex[maxn][30];
char s[maxn];
void solve()
{
    memset(nex,-1,sizeof(nex));
    int len=strlen(s+1);
    for(int i=len-1;i>=0;i--)
        for(int j=0;j<26;j++)
            if(s[i+1]=='a'+j)
                nex[i][j]=i+1;
            else
                nex[i][j]=nex[i+1][j];
 
    memset(dp,0x3f3f3f3f,sizeof(dp));
    for(int i=len;i>=0;i--)
    {
        bool flag=false;
        for(int j=0;j<26;j++)
            if(nex[i][j]==-1)
            {
                dp[i]=1;
                flag=true;
                break;
            }
        if(flag) continue;
        for(int j=0;j<26;j++)
            if(nex[i][j] != -1)
                dp[i]=min(dp[i],dp[nex[i][j]]+1);
    }
 
    for(int i=0;i<=len;)
    {
        if(dp[i]==1)
        {
            for(int j=0;j<26;j++)
                if(nex[i][j]==-1)
                {
                    printf("%c",j+'a');
                    break;
                }
            break;
        }
 
        for(int j=0;j<26;j++)
        {
            if(nex[i][j]!=-1 && dp[i]==dp[nex[i][j]]+1)
            {
                printf("%c",j+'a');
                i=nex[i][j];
                break;
            }
        }
    }
}
int main()
{
    scanf("%s",s+1);
 
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/81282330