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的开头字符加 ...
随机推荐
- Font Awesome矢量图标
下载 font-awesome 文件夹到您的项目中. 在HTML的 <head> 中引用font-awesome.min.css. 可以将Font Awesome图标使用在几乎任何地方,只 ...
- fread/fwrite实现复制功能
1. fread/fwrite实现复制功能 #include <stdio.h> #include <stdlib.h> #define BUFFSIZE 4096 //执行 ...
- ACM_01背包(恰好装满)
背包2 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量刚好为 W ...
- Jax
The scope of this project is to automate the current Credit Correction process of opening, editing, ...
- PS如何批量整理图片大下
https://jingyan.baidu.com/article/cdddd41cc7849853cb00e193.html
- Http请求1
package Test; import java.io.IOException; import java.io.InputStreamReader; import java.net.URISynta ...
- linux centos7 安装nginx并启动
Linux下安装Nginx完整教程及常见错误解决方案:https://blog.csdn.net/chenxiaochan/article/details/63688346 CentOS 7 安装Ng ...
- redis的安装、启动、主从配置,以及.Net下StackExchange.Redis的使用
开门见山,Linux下配个环境真是苦逼死了,这里记录一下,囧 一.环境 服务端:Ubuntu16.04 LTS(虚拟机,redis官方没有window发布版本,而且在Linux下运行更稳定) 客户端: ...
- Java 基础入门随笔(4) JavaSE版——程序流程控制
上一节对于运算符有了大致的了解,这一节针对程序流程控制进行复习!程序流程控制包括顺序结构.判断结构(if).选择结构(switch).循环结构. 1.判断结构 ①if语句的第一种格式: ...
- tomcat 访问IP直接访问项目
apache-tomcat-7.0.52\conf下server.xml文件 <Connector connectionTimeout="20000" port=" ...