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 }

 

posted @ 2017-04-17 09:55  樱花落舞  阅读(389)  评论(0编辑  收藏  举报