• 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 }
  • 相关阅读:
    hmac模块
    hashlib模块
    内存监控
    在全局对象(不是指针)的构造函数里不要对std集合做太多操作
    Lucene 4.X 倒排索引原理与实现
    Git工作流指南
    Spring cloud 框架 --- Eureka 心得
    分布式 的理解
    集群的理解
    Thrift框架-安装
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6721488.html
Copyright © 2020-2023  润新知