http://codeforces.com/problemset/problem/197/C
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.
The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.
Print the lexicographically maximum subsequence of string s.
ababba
bbba
abbcbccacbbcbaaba
cccccbba
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;
}