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的开头字符加 ...
随机推荐
- Android 性能优化(7)网络优化( 3) Optimizing User-Initiated Network Use
Optimizing User-Initiated Network Use This lesson teaches you to Pre-fetch Network Data Check for Co ...
- dos 下小tip
tip 1:日期的格式化 方法如下: Echo %Date:~0,4%%Date:~5,2%%Date:~8,2%或者Set dt=%Date:~0,4%%Date:~5,2%%Date:~8,2%E ...
- 381 Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复
设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构.注意: 允许出现重复元素. insert(val):向集合中插入元素 val. remove(val):当 val ...
- WCF wsdlexception(at/html):faultCode=INVALID_WSDL
WCF 部署正常,通过浏览器查看服务也OK,但是通过SOAP UI创建客户端请求时就异常: wsdlexception(at/html):faultCode=INVALID_WSDL: Expecte ...
- SQL函数类的操作,增加,查询
数据库连接: 表的创建: 创建连接对象,命令对象类: 添加函数: 查询函数类: List<>集合,里面专门放对象 函数主体: 查询: foreach只能修改,不能添加删除
- datagrid上面的查询按钮设置了,但是分页工具栏不显示
原因:查询的linkbutton没有放在toolbar里. <script type="text/javascript"> $(function(){ $('#dg') ...
- redis学习-sds数据类型
今天开始了redis的学习,本来想直接从源码看起的,不过看到有篇对redis介绍的基础教程 <Redis 设计与实现(第一版)> 于是决定从这个开始入门. 1.数据类型定义 typedef ...
- Meta标签 h5
一 PC端meta标签 1 页面关键词 <meta name="keywords" content="your tags"> 2 页面描述 < ...
- Java集合框架源码(一)——hashMap
注:本人的源码基于JDK1.8.0,JDK的版本可以在命令行模式下通过java -version命令查看. 一首先我们来看一下HashMap类的定义: public class HashMap< ...
- Concurrent control in SQLite
This document describes the technologies to concurrent access to a SQLite database. There are also s ...