链接:
https://www.nowcoder.com/acm/contest/84/A
来源:牛客网
来源:牛客网
题目描述
给定字符串s,s只包含小写字母,请求出字典序最大的子序列。
子序列:https://en.wikipedia.org/wiki/Subsequence
字典序:https://en.wikipedia.org/wiki/Lexicographical_order
子序列:https://en.wikipedia.org/wiki/Subsequence
字典序:https://en.wikipedia.org/wiki/Lexicographical_order
输入描述:
一行一个字符串s (1 <= |s| <= 100,000)。
输出描述:
字典序最大的子序列。
#include<bits/stdc++.h>
using namespace std;
char ch[100002], s[100002];
int main()
{
cin>>ch;
int length = strlen(ch);
int pos = 0, k = 0, flag = 0;
for(int i = 'z'; i >= 'a'; i--)
{
for(int j = pos; j < length; j++)
{
if(ch[j] == i)//尽可能匹配大的 匹配到一个当前最大的之后 再从后面剩余的找出尽可能大的 一直找到最后一位即可
{
s[k] = ch[j];
k++;
pos = j;//在下轮循环中以当前的pos往后找
}
}
}
for(int i = 0; i < k; i++)
{
cout<<s[i];
}
cout<<endl;
return 0;
}