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.

题意:求一个最短并且字典序最小的,非str的子序列的字符串。

从后往前,当26个字母都出现一次的时候分成一组,把这个序列分成多组

如果刚好可以分成n组那么输出字串的长度为n+1并且第一个字符一定是‘a’。

否则的话长度为n,第一个字符为最前面一组中没有出现的字符。

在下一组找到上一个选的字符的位置,找出这一组后面没有出现的最小的字符,重复直到结束。

 

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
int vis[30], R[200005] = {0};
char str[200005];
int main(){
	scanf("%s", &str);
	int n = strlen(str);
	R[0] = n - 1;
	int t = 0, k = 1;
	for(int i = n - 1; i >= 0; i--){
		if(!vis[str[i] - 'a']){
			t++;
			vis[str[i] - 'a'] = 1;
		}
		if(t == 26){
			t = 0;
			memset(vis, 0, sizeof(vis));
			R[k++] = i - 1;
		}
	}
	char temp;
	
	if(R[k - 1] == -1){
		temp = 'a';
		printf("a");
	}
	else{
		for(int i = 0; i < 26; i++){
			if(!vis[i]){
				temp = 'a' + i;
				printf("%c", temp);
				break;
			}
		}
	}
	int Index = k - 2;
	while(Index >= 0){
		memset(vis, 0, sizeof(vis));
		for(int i = R[Index + 1] + 1; i <= R[Index]; i++){
			if(str[i] == temp){
				t = i;
				break;
			}
		}
		
		for(int i = t + 1; i <= R[Index]; i++){
			vis[str[i] - 'a'] = 1;
		}
		
		for(int i = 0; i < 26; i++){
			if(!vis[i]){
				temp = 'a' + i;
				printf("%c", temp);
				break;
			}
		}
		Index--;
	}
	puts("");
	
	return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值