• Educational Codeforces Round 19 C


    Description

    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
    题意:第一个字符串的首字母放在第二个字符串的中,第二个字符串的结尾再输出,求能够得到字典序最小的字符串
    解法:模拟,如果第一个字符串的首字母为最小,则直接输出,否则压入栈内,处理完毕之后再输出
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int num[30];
     4 string s;
     5 int check(char c)
     6 {
     7     for(int i='a';i<c;i++)
     8     {
     9         if(num[i])
    10         {
    11             return 0;
    12         }
    13     }
    14     return 1;
    15 }
    16 stack<char>q;
    17 int main() {
    18     ios::sync_with_stdio(false);
    19     cin.tie(0);
    20     cin>>s;
    21     for(int i=0;i<s.size();i++)
    22     {
    23         num[s[i]]++;
    24     }
    25     int cnt=0;
    26     while(cnt<s.size())
    27     {
    28         if(q.empty())
    29         {
    30             q.push(s[cnt]);
    31             num[s[cnt]]--;
    32             cnt++;
    33         }
    34         else if(check(q.top()))
    35         {
    36             cout<<q.top();
    37             q.pop();
    38         }
    39         else
    40         {
    41             q.push(s[cnt]);
    42             num[s[cnt]]--;
    43             cnt++;
    44         }
    45     }
    46     while(!q.empty())
    47     {
    48         cout<<q.top();
    49         q.pop();
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    lua 函数调用1 -- 闭包详解和C调用
    lua API 小记2
    Lua API 小记1
    unity3d开发环境配置
    Hash表
    如何优化Cocos2d-X游戏的内存
    openGL光源概念
    Cocos2D-X屏幕适配新解
    Lua脚本在C++下的舞步
    lua table操作实例详解
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6721488.html
Copyright © 2020-2023  润新知