对于字符串x和y, 如果擦除x中的某些字母(有可能全擦掉或者都不擦)能够得到y,我们就称y是x的子序列。例如."ncd"是"nowcoder"的子序列,而"xt"不是。
现在对于给定的一个字符串s,请计算出字典序最大的s的子序列。
输入描述:
输入包括一行,一个字符串s,字符串s长度length(1 ≤ length ≤ 50). s中每个字符都是小写字母
输出描述:
输出一个字符串,即字典序最大的s的子序列。
示例1
输入
test
输出
tt
知识点:C++ STL,字符操作
#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
ostringstream ss;
while(!s.empty()){
string::iterator it = max_element(s.begin(),s.end());
ss<<*it;
s.erase(s.begin(),it+1);
}
cout<<ss.str()<<endl;
return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main
{
static StringBuilder solve(String x)
{
StringBuilder y=new StringBuilder();
y.append(x.charAt(x.length()-1));
int i=x.length()-2;
while(i>=0)
{
if(x.charAt(i)>=y.charAt(y.length()-1))
y.append(x.charAt(i));
i--;
}
return y.reverse();
}
public static void main(String[] args) throws IOException
{
BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
String x= scanner.readLine();
System.out.println(solve(x));
}
}