UPC6597 Don't Be a Subsequence(求最短子序列)

题目描述

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.

想法:不会做,做梦也想不到( ̥́ ˍ ̀ू )

思路:要求的是不是子序列的最短的并且字典序最小的序列
不要求连续,但是前后关系不能变
从后向前划分,遇到26种(种!种!种!!!)划分一段,假设划分了整整 三段,那么最小子序列长度一定大于三,从最后一段不满的序列中选出第一个没有的字母比如说a,在接下来满的区间中,找第一个a后面没有出 现的单词,依次下去,即可求出所要求序列

需要解决的问题:1.区间的划分
2.找第一个XX单词后第一个没出现的单词
解决:1.从后先前遍历:遇到一个 新单词就sum++
2.从后向前遍历:遇到需要遇到的单词记住坐标,再往前遍历时,要是再遇到就更新

#include<bits/stdc++.h>
#define ll long long
using namespace std;
char str[200005];
int vis[26]={0};
int s[200005][26]={0},ss[200005][26]={0};
int main()
{
    str[0]='A';
    scanf("%s",str+1);
    int n=strlen(str)-1;
    int k=0,sum=0;//k记录第几个区间 
    //printf("n=%d\n",n);
    for(int i=n;i>0;i--)
    {
        s[k][str[i]-'a']=i;//在第k区间内某字母首次出现的位置 

        for(int j=0;j<26;j++)
        ss[i][j]=vis[j];//从i位置开始哪些字母是否出现过,出现过ss[i][j]为1,没为0; 
        if(vis[str[i]-'a']==0)
        {
            vis[str[i]-'a']++;
            sum++; 
        }
        if(sum==26)//26种全了就区间更新 
        {
            sum=0;
            k++;
            memset(vis,0,sizeof(vis));
        }
    //  vis[s[i]-'a'] = i;
    }
//  printf("sum=%d\n",sum);
    for(int i=0;i<26;i++)//从前往后找 
    {
        if(s[k][i]==0)//某字母第k个区间内没出现 
        {
            int m=i;
            printf("%c",'a'+i);
            while(k--)
            {
                int j=s[k][m];
                for(int t=0;t<26;t++)
                {
                    if(ss[j][t]==0)
                    {
                        printf("%c",'a'+t);
                        m=t;
                        break;
                    }
                }
            }
            break;
        }
    }
    printf("\n");
    return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值