Minimal string 栈 贪心
1 second
256 megabytes
standard input
standard output
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 lexicographically minimal.
You should write a program that will help Petya win the game.
First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters.
Print resulting string u.
cab
abc
acdb
abdc
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100009
#define N 40
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0);
/*
从前往后遍历
要让字典序的顺序尽量小,那么如果该字符后面有字典序比它小的,应当一直压栈直到栈顶元素优先级更高为止。(保证字典序小的优先!)
*/
char s[MAXN],best[MAXN];
stack<char> S;
int main()
{
scanf("%s", s);
int l = strlen(s);
best[l] = 'z';
int p = ;
for (int i = l - ; i >= ; i--)
best[i] = min(s[i], best[i + ]);
while (!S.empty() || p < l)
{
if (!S.empty() && S.top() <= best[p])
putchar(S.top()), S.pop();
else
S.push(s[p++]);
}
printf("\n");
return ;
}
Minimal string 栈 贪心的更多相关文章
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- [cf797c]Minimal string(贪心+模拟)
题意: 给出了字符串s的内容,字符串t,u初始默认为空,允许做两种操作: 1.把s字符串第一个字符转移到t字符串最后 2.把t字符串最后一个字符转移到u字符串最后 最后要求s.t字符串都为空,问u字符 ...
- CodeForce-797C Minimal string(贪心模拟)
Minimal string CodeForces - 797C Petya 收到一个长度不超过 105 的字符串 s.他拿了两个额外的空字符串 t 和 u 并决定玩一个游戏.这个游戏有两种合法操作: ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- Minimal string CodeForces - 797C
Minimal string CodeForces - 797C 题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的 ...
- Minimal string CodeForces – 797C
题目链接 题目难度: 1700rating 题目类型:string+贪心+STL 题目思路: 由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在 ...
- D、CSL 的字符串 【栈+贪心】 (“新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛)
题目传送门:https://ac.nowcoder.com/acm/contest/551#question 题目描述 CSL 以前不会字符串算法,经过一年的训练,他还是不会……于是他打算向你求助. ...
- ural 1303 Minimal Coverage【贪心】
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...
- 【codeforces 797C】Minimal string
[题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...
随机推荐
- Oracle 参考脚本
一.创建物化视图 --新建表空间 CREATE TABLESPACE MLOG_TBS LOGGING DATAFILE 'mlog_tbs.dbf' SIZE 32M AUTOEXTEND ON N ...
- JavaScript 把函数作为参数进行传值
JavaScript 响应式编程模式有点类似 WebForm 中的事件驱动模式(传相应的处理函数给委托,通过事件来触发来进行相关的处理),在 AngularJs 2.x 框架中,应用了 RxJS AP ...
- 每天学点Linux命令:倒叙打印文件第二行的前100个大写字母
sed -n | rev 处理第二行 grep:提取大写字母 o: 不显示非结果 tr:删除换行 Cut:截取1-100个字符 rev:逆序 断断续续搞了好长时间. ...
- Intellij使用心得(四) -- 导入Eclipse的代码格式化文件
https://my.oschina.net/flashsword/blog/137598
- CF861B Which floor?
思路: 暴力枚举. 实现: #include <bits/stdc++.h> using namespace std; int n, m, x, y; bool check(int x, ...
- leetcode375 Guess Number Higher or Lower II
思路: dp. https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/ 实现: class Solution { ...
- 联想 S5 Pro GT(L78091)免解锁BL 免rec 保数据 ROOT Magisk Xposed 救砖 ZUI5.0.047
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- java设计模式之代理模式模式总结
定义:代理模式这种设计模式是一种使用代理对象来执行目标对象的方法并在代理对象中增强目标对象方法的一种设计模式. 解读定义: 1.代理对象和目标对象有共同的接口: 2.使用代理对象执行目标对象中的方法: ...
- XML解析——Java中XML的四种解析方式(转载 by 龍清扬)
XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已. XML的解析方式分为四 ...
- crm01 Django-admin管理工具
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...