首页 > 其他 > 详细

[CF797C] Minimal string - 贪心,栈

时间:2020-04-30 11:14:06      阅读:59      评论:0      收藏:0      [点我收藏+]

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;
    }
}

[CF797C] Minimal string - 贪心,栈

原文:https://www.cnblogs.com/mollnn/p/12806716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!