[cf797c]Minimal string(贪心+模拟)
题意:
给出了字符串s的内容,字符串t,u初始默认为空,允许做两种操作:
1、把s字符串第一个字符转移到t字符串最后
2、把t字符串最后一个字符转移到u字符串最后
最后要求s、t字符串都为空,问u字符串字典序最小能是多少。
解题关键:
主要就是贪心,按字典序,每贪心完一个字母,往前回溯一次。
1、hash一下每个字母出现的次数,然后贪心选择字典序最小的即可。
2、预处理每个位置能达到的最小的字母,然后贪心。tmp一定是一个单调不减的数组。
3、小于等于而不是等于的原因是abacd这种情况。
反思:
1、这种看似简单的模拟题一定要搞明白,自己写一下。类似于栈混洗。
2、string的这种类似java和python的用法
法1:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
stack<char>s1;
int m1[];
int cnt=;
char s2[];
int main(){
string s;
int nn=;
cin>>s;
for(int i=;s[i];i++) m1[s[i]-'a']++;
while(!m1[nn]&&nn<)nn++;
for(int i=;s[i];i++){
if(s1.empty()||m1[nn]){
s1.push(s[i]);
m1[s[i]-'a']--;
}
while(!s1.empty()&&s1.top()<=nn+'a'){//改成小于等于就过了
s2[cnt++]=s1.top(),s1.pop();
while(!m1[nn]&&nn<)nn++;
}
while(!m1[nn]&&nn<)nn++;
}
while(!s1.empty()) s2[cnt++]=s1.top(),s1.pop();
printf("%s\n",s2);
}
法二:
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
stack<char>ss;
char tmp[],x;
int main(){
string s;
cin>>s;
x='z'+;
for(int i=s.size()-;i>=;i--) x=min(x,s[i]),tmp[i]=x;//tmp[i]代表该位置能使结果到达最小的值
string ans="";
for(int i=;i<s.size();i++){
while(!ss.empty()&&ss.top()<=tmp[i]) ans+=ss.top(),ss.pop();
ss.push(s[i]);
}
while(!ss.empty()) ans+=ss.top(),ss.pop();
cout<<ans<<"\n";
return ;
}
[cf797c]Minimal string(贪心+模拟)的更多相关文章
- CodeForce-797C Minimal string(贪心模拟)
Minimal string CodeForces - 797C Petya 收到一个长度不超过 105 的字符串 s.他拿了两个额外的空字符串 t 和 u 并决定玩一个游戏.这个游戏有两种合法操作: ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- Minimal string CodeForces – 797C
题目链接 题目难度: 1700rating 题目类型:string+贪心+STL 题目思路: 由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在 ...
- 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的 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Minimal string 栈 贪心
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- 在ios开发中使用 try 和 catch 来捕获错误。
本文转载至 http://blog.csdn.net/remote_roamer/article/details/7105776 抛出错误的代码 //如果返回的报文是错误信息,则抛出错误 if([ou ...
- python 基础 7.4 os 模块
#/usr/bin/python #coding=utf8 #@Time :2017/11/11 3:15 #@Auther :liuzhenchuan #@File :os 模块.py im ...
- ThreadLocal的简单使用
package com.thread; public class ThreadLocalTest { public static void main(String[] args) { final Pe ...
- runsv
runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...
- lamp环境的搭建和配置
安装apache httpd-2.2.31.tar.gz rpm -qa|grep httpd ##卸载旧的httpd httpd--.el6.centos.x86_64 httpd-tools- ...
- mysql系列之1.mysql基础
非关系型(NOSQL)数据库 键值存储数据库: memcached / redis / memcachedb / Berkeley db 列存储数据库: Cassandra / Hba ...
- SAP RFC 的介绍
第一部分 RFC技术 什么是RFC? RFC是SAP系统和其他(SAP或非SAP)系统间的一个重要而常用的双向接口技术,也被视为SAP与外部通信的基本协议.简单地说,RFC过程就是系统调用当前系统外的 ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- 怎么样写一个能告诉你npm包名字是否被占用的工具
事情是这样的: 因为我经常会写一些npm包,但是有时候我写完一个包,npm publish 的时候却被提示说包名字被占用了,要不就改名字,要不就加scope,很无奈.npm 命令行可以通过 npm v ...
- IIS反向代理实现Revel域名访问
Revel实现域名访问 1.在cmd中启动revel项目,我设置的端口为8000 2.下载IIS的Application Request Routing Cache组件下载地址 3.安装ARR 4.打 ...