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

35 篇文章 0 订阅

问题 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

提示

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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值