zoukankan      html  css  js  c++  java
  • Codeforces 797C

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


  • 相关阅读:
    【文字检测算法整理】
    【LDA】周志华
    【PCA】周志华
    【SVM】周志华
    4.1、顺序栈的实现(java实现)
    3.1、双向循环链表(java实现)
    5、链表队列(java实现)
    4、链栈的实现(java代码)
    3、循环链表(java实现)
    CommonsMultipartFile 转为 File 类型
  • 原文地址:https://www.cnblogs.com/widsom/p/6720495.html
Copyright © 2011-2022 走看看