UPC6597: Don't Be a Subsequence(贪心+思维)

6597: Don't Be a Subsequence

时间限制: 1 Sec  内存限制: 128 MB
提交: 209  解决: 34
[提交] [状态] [讨论版] [命题人: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.

题意:输入一串字符串,找出最小的字典序的一个字符串不是他的子序列

可以说是3.5个小时被序列搞得wongwong的了,从后面扫一遍将字符串分成若干区间,分的条件是如果26个字符都出现了一次则为一个区间,并且用一个二维数组记录每个位置后面的  a—z在第几个位置,用一个一位数组及记录每个位置属于第几个区间,tot为区间的总个数,则这个子串的长度一定是区间的个数加1(区间个数不到一个为0),才能保证任何这个长度的子串不属于他的子序列。接下来就是贪心了,最前面的一定是一个不完全区间找到最小的没有出现的则这个字母打头一定是最小的字典序序列,然后找到这个字母出现位置的第一次,如果他的下个a-z字母跟他不在一个区间里那么下一个一定是这个字母,这里可以贪心从a开始寻找。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define FIN freopen("D://code//in.txt", "r", stdin)
#define ppr(i,x,n) for(int i = x;i <= n;i++)
#define rpp(i,n,x) for(int i = n;i >= x;i--)
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;

inline int read() {//读入挂
    int ret = 0, c, f = 1;
    for(c = getchar(); !(isdigit(c) || c == '-'); c = getchar());
    if(c == '-') f = -1, c = getchar();
    for(; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
    if(f < 0) ret = -ret;
    return ret;
}
int nextt[maxn][26];
int now[26],area[maxn];
int tot,tmp;
set<int >S;
int main()
{
	IO;
	tot = 0;
	string s;
	string ans;
	cin>>s;
	rpp(i,s.size()-1,0)
	{
		int gg = s[i] - 'a';
		S.insert(gg);
		area[i] = tot;
		if(S.size() == 26)
		{
			tot++;
			S.clear();
		}
	}
	if(tot == 0)
	{
		ppr(i,0,25)
		{
			if(S.count(i) == 0)
			{
				char minn= i+'a';
				cout<<minn<<endl;
				return 0;
			}
		}
	}
	rpp(i,s.size()-1,0)
	{
		ppr(j,0,25)
		{
			nextt[i][j] = now[j];
		}
		now[s[i] - 'a'] = i;
	}
	ppr(i,0,25)
	{
		if(S.count(i) == 0)
		{
			ans += (i + 'a');
			tmp = now[i];
			break;
		}
		
	}
	ppr(i,1,tot)
	{
		ppr(j,0,25)
		{
			if(area[nextt[tmp][j]] == area[tmp]-1)
			{
				char minn = j + 'a';
				ans += minn;
				tmp = nextt[tmp][j];
				break;
			}
		}
	}
	ppr(i,0,25)
	{
		if(nextt[tmp][i] == 0)
		{
			char minn = i + 'a';
			ans += minn;
			break;
		}
	}
	cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/Pandapan1997/article/details/81266659