fork(1) download
#include <iostream>
#include <algorithm>
using namespace std;

string findLDS(const string& sequence) {
    int n = sequence.size();

    // dp[i] 表示以 sequence[i] 结尾的最大递减子序列的长度
    int dp[50] = {0};

    // 记录最大递减子序列的末尾位置
    int endPos[50] = {-1};

    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            if (sequence[j] >= sequence[i] && dp[j] + 1 > dp[i]) {
                dp[i] = dp[j] + 1;
                endPos[i] = j;
            }
        }
    }
	cout << dp << endl;
    // 找到最大递减子序列的末尾位置
    int maxLen = 0, maxEndPos = 0;
    for (int i = 0; i < n; ++i) {
        if (dp[i] > maxLen) {
            maxLen = dp[i];
            maxEndPos = i;
        }
    }

    // 构造最大递减子序列
    string result;
    while (maxEndPos != -1) {
        result += sequence[maxEndPos];
        maxEndPos = endPos[maxEndPos];
    }

    reverse(result.begin(), result.end());
    return result;
}

int main() {
    string sequence;
    bool firstOutput = true; // 用于标记是否为第一次输出结果
    while (cin >> sequence) {
        string LDS = findLDS(sequence);
        if (!firstOutput) {
            cout << endl;
        } else {
            firstOutput = false;
        }
        cout << LDS;
    }
    return 0;
}
Success #stdin #stdout 0.01s 5280KB
stdin
5687643821
456879421
stdout
0x7ffcaf3ee4a0
587643210x7ffcaf3ee4a0

487421