AtCoder Regular Contest 081 E (upc 6597): Don't Be a Subsequence

22 篇文章 0 订阅

题目链接:https://arc081.contest.atcoder.jp/tasks/arc081_c

题目描述

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.

题意:求一个串的最短非子串,如果有多个输出字典序最小的那个。

思路:

刚开始默认所有的点的Next[len][j] = len 

Next[i][j] 是以 i 开始 到len  j+'a' 出现的临近 i 的最早位置 。 如果对于某个字符i之后没出现过 那么它出现的位置就等于len 就是Next刚开始初始化的位置。对于一 i 之后没出现过该字母的话 那么对于当前 dp[i] 来说 它的最小值一定等于某个没出现的字符的dp值,因为没出现他的 dp[X] 就会等于1,如果出现过肯定是找一个出现次数最少的,每次都是最少的这样会让DP最小化,然后输出的就逆序,怎么过来的再怎么回去就行了。通过DP方程返回去求解即可。

#include<bits/stdc++.h>
using namespace std;
char str[200005];
int dp[200005];
int Next[200005][30];
int main()
{
	scanf("%s",str);
	int len=strlen(str);
	for(int i=0;i<26;i++)//以len结尾的i出现的位置 
		Next[len][i]=len;
		
	for(int i=len-1;i>=0;i--)
		for(int j=0;j<26;j++)//i开头的子串中 j+'a'第一次出现的位置 
			Next[i][j]=((str[i]==(j+'a'))?i:Next[i+1][j]);
	
	dp[len]=1;
	for(int i=len-1;i>=0;i--)
	{
		dp[i] = dp[Next[i][0]+1]+1;
		for(int j=1;j<26;j++)
			dp[i] = min (dp[i], dp[Next[i][j]+1]+1);
	}
	
	int flag=0;
	while(dp[flag])
	{
		for(int i=0;i<26;i++)
		{
			if(dp[flag]==dp[Next[flag][i]+1]+1)
			{
				printf("%c",'a'+i);
				flag=Next[flag][i]+1;
				break;
			}
		}
	}
	printf("\n");
	return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值