洛谷 CF196A 题解

题目传送门

题目描述:

读入字符串,求该串的最大字典序子序列

我的思路:

循环判断只要当前字符比后面所有的字符的字典序大,就把这个字符存到另一个字符串中,最后和输入的字符串循环比较如果该字符和输入时的是相同的,就输出。

注意:

因为比较字符串的字典序是要从第一个字符开始依次往后比较的,所以要想取到最大字典序子序列,就要倒着查找!

Code: #include <bits/stdc++.h> //万能头文件 using namespace std; string st; char t[100005]; int a[100005]; int main(){ cin>>st; //读入字符串 int len=st.length(); for(int i=len-1;i>=0;i--){ t[i]=max(t[i+1],st[i]); } for(int i=0;i<len;i++){ if(t[i]==st[i]) cout<<st[i]; } return 0; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsjfs.html