题目:
题解:
本质就是求最长上升子序列,只不过这里是字符串版本的,我们都知道有n^2的LIS,但其实还有O(nlogn)版本的,详细看这里,套上就行
另外我发现这里竟然有蓝桥杯全套的编程题离谱,贴上本题地址
代码:
#include <iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
string a[maxn];
string b[maxn];
int pos[maxn], ans[maxn];
int main()
{string s;cin >> s;int cnt = 0;for (int i = 0; i < s.size(); i++) {if (s[i] >= 'A' && s[i] <= 'Z') a[++cnt] = s[i];else a[cnt] += s[i];}int len = 1;b[1] = a[1];pos[1] = len;for (int i = 2; i <= cnt; i++) {if (a[i]>b[len]) {//a大于bb[++len] = a[i];//加入bpos[i] = len;//a中第i个数在b中的位置}else {int id = lower_bound(b + 1, b + 1 + len, a[i]) - b;//找到b中大于a[i]的第一个位置b[id] = a[i];//该位置用当前a替换pos[i] = id;}}int tmp = len;string maxx = "Zzzzzzzzzz";for (int i = cnt; i >= 1; i--) {if (!tmp)break;if (pos[i] == tmp && maxx>a[i]) {ans[tmp] = i;--tmp;maxx = a[i];}}for (int i = 1; i <= len; i++) {cout << a[ans[i]];}cout << endl;return 0;
}