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 @   樱花落舞  阅读(391)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2016-04-17 Codeforces Round #347 (Div. 2) A
2016-04-17 大数模版
点击右上角即可分享
微信分享提示