[CF797C] Minimal string

Description

给出一个字符串,按照从前到后的顺序进栈,输出字典序最小的出栈序列

Solution

栈顶元素比未入栈的所有元素都小(不严格)时才出栈

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

char a[N],mx[N],s[N];
int n,top;

signed main() {
    ios::sync_with_stdio(false);
    cin>>a+1;
    n=strlen(a+1);
    mx[n+1]=127;
    for(int i=n;i>=1;--i) mx[i]=min(a[i],mx[i+1]);
    for(int i=1;i<=n;i++) {
        s[++top]=a[i];
        while(top && s[top]<=mx[i+1]) cout<<s[top], --top;
    }
}
原文地址:https://www.cnblogs.com/mollnn/p/12806716.html