Codeforces 197C 找最大子串(字典序 可以不连续)

本文介绍了一种算法,用于寻找给定字符串中字典序最大的子序列。通过逆向遍历字符串并记录最大字符的方式,高效地解决了这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/problemset/problem/197/C


C. Lexicographically Maximum Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.

We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 < p2 < ... < pk ≤ |s|) a subsequence of string s = s1s2... s|s|.

String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ... , x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. Characters in lines are compared like their ASCII codes.

Input

The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.

Output

Print the lexicographically maximum subsequence of string s.

Sample test(s)
Input
ababba
Output
bbba
Input
abbcbccacbbcbaaba
Output
cccccbba
Note

Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).

The first sample: aBaBBA

The second sample: abbCbCCaCbbCBaaBA



题意  :

输入一个字符串  找出这个字符串按字典序排列的最大子串

1 ≤ p1 < p2 < ... < pk ≤ |s|   根据这个条件可以看出 可以是不连续的子串  千万注意细节条件  仔细分析每一个条件   大多数时候每一个条件都有肯定的作用

所以 千万注意细节  提取出每个条件



思路:

很容易看出 假如  abcaaccbcac

首先遍历一遍找到最大字符c 最大字串的第一个字符 设为c 再从其后开始 即aaccbcac

再遍历一遍找到最大字符c  最大字串的第二个字符 设为c  如此下去 直到最后一个  即可

直接暴力显然会超时

有stable_sort   仍旧会超时


这时候可以用逆向思维 从后面往前找   

By hnust_xiehonghao, contest: Codeforces Round #124 (Div. 2), problem: (C) Lexicographically Maximum Subsequence, Accepted, #

#include<stdio.h>
#include<string.h>
char s[1000000];
char ans[1000000];
int main()
{
	int i,len,cnt;
	char max=0;
    while(scanf("%s",s)!=EOF)
	{
		max=0;cnt=1000000-1;
		len=strlen(s);
		for(i=len-1;i>=0;i--)
		{
			if(s[i]>=max) {max=s[i]; ans[cnt--]=s[i];}
		}
	//	puts(ans);
		//ans[cnt]=0;
		for(i=cnt+1;i<1000000;i++)
			printf("%c",ans[i]);
		printf("\n");
		 
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值