天天看點

求字典序最大的子序列(STL中max_element與string的erase函數的應用)

題目描述

對于字元串x和y, 如果擦除x中的某些字母(有可能全擦掉或者都不擦)能夠得到y,我們就稱y是x的子序列。例如."ncd"是"nowcoder"的子序列,而"xt"不是。

現在對于給定的一個字元串s,請計算出字典序最大的s的子序列。

輸入描述:

輸入包括一行,一個字元串s,字元串s長度length(1 ≤ length ≤ 50).

s中每個字元都是小寫字母

輸出描述:

輸出一個字元串,即字典序最大的s的子序列。

示例1

輸入

複制

test

輸出

複制

tt

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
    string Input;
    cin >> Input;
    string Result;
    int index = 0;
    while(!Input.empty()){
        auto it = max_element(Input.begin(),Input.end());
        Result = Result + *it;
        Input.erase(Input.begin(),it + 1);
    }
    cout << Result << endl;
    return 0;
}