Codeforces 797C - Minimal string

C. Minimal string
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves:

  • Extract the first character of s and append t with this character.
  • Extract the last character of t and append u with this character.

Petya wants to get strings s and t empty and string u lexigraphically minimal.

You should write a program that will help Petya win the game.

Input

First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters.

Output

Print resulting string u.

Examples
input
cab
output
abc
input
acdb
output
abdc
题目大意:略。
方法:栈顶的优先级大于后缀的优先级,则出栈。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int N=1e5+5;
int main()
{
    char s[N];
    gets(s);
    int n=strlen(s);
    char best[N];
    best[n]='z';
    for(int i=n-1;i>=0;i--)
    {
        best[i]=min(best[i+1],s[i]);
    }
    stack<char>v;
    int cur=0;
    while(!v.empty()||cur<n)
    {
        if(!v.empty()&&v.top()<=best[cur])
        {
            putchar(v.top());
             v.pop();
        }
        else v.push(s[cur++]);
    } 
    cout<<endl;
    return 0;
}

 



转载于:https://www.cnblogs.com/widsom/p/6720495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值