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
题意:第一个字符串的首字母放在第二个字符串的中,第二个字符串的结尾再输出,求能够得到字典序最小的字符串
解法:模拟,如果第一个字符串的首字母为最小,则直接输出,否则压入栈内,处理完毕之后再输出
 #include <bits/stdc++.h>
using namespace std;
int num[];
string s;
int check(char c)
{
for(int i='a';i<c;i++)
{
if(num[i])
{
return ;
}
}
return ;
}
stack<char>q;
int main() {
ios::sync_with_stdio(false);
cin.tie();
cin>>s;
for(int i=;i<s.size();i++)
{
num[s[i]]++;
}
int cnt=;
while(cnt<s.size())
{
if(q.empty())
{
q.push(s[cnt]);
num[s[cnt]]--;
cnt++;
}
else if(check(q.top()))
{
cout<<q.top();
q.pop();
}
else
{
q.push(s[cnt]);
num[s[cnt]]--;
cnt++;
}
}
while(!q.empty())
{
cout<<q.top();
q.pop();
}
return ;
}

最新文章

  1. 2.0 (2)测试pymongo
  2. String与InputStream相互转换
  3. js实现缓冲运动,和匀速运动有点不相同
  4. 《ASP.NET1200例》解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”
  5. CSS动画控制器
  6. Hadoop的HA集群启动和停止流程
  7. phonegap 检查是否有网络
  8. 数据库里面DataTime时间类型字段,如果为null时
  9. 转 如何不耍流氓的做运维之——SHELL脚本
  10. TCP 滑动窗口
  11. Servlet--SingleThreadModel接口,RequestDispatcher接口
  12. Lintcode395 Coins in a Line II solution 题解
  13. SVM算法简单应用
  14. lambda List实现某列去重的解决方案采用扩展方法
  15. Nginx 配置下载附件让浏览器提示用户是否保存
  16. 随机生成游戏用户昵称(nodejs版本)(含机器人头像,金币等)
  17. Django之Models(三)
  18. 使用FreeMarker生成word文档
  19. EZ 2018 05 26 NOIP2018 模拟赛(十六)
  20. URL List by Category

热门文章

  1. busybox 终端支持 ctrl-r
  2. T-SQL查询进阶--变量
  3. sanic官方文档解析之蓝图
  4. 6.游戏特别离不开脚本(4)-应该避免将集合框架对象传给JS
  5. Studio 3T for MongoDB连接51.212复制集
  6. Axure Base 01
  7. ICMP协议 广播以查询局域网内的所有主机
  8. react项目中的注意点
  9. POJ2955 Brackets —— 区间DP
  10. codeforces 435 B. Pasha Maximizes 解题报告