博客
关于我
C. Minimal string(思维+贪心+细节)
阅读量:241 次
发布时间:2019-03-01

本文共 815 字,大约阅读时间需要 2 分钟。


题意:给出一个字符串,按照从前到后的顺序进栈,输出字典序最小的出栈序列

思路:最开始贪心,预处理一个后缀,看是否存在比栈顶更小的。有就按下不放出来。然后wa17...以为贪心不行。改成一个处理每个位置的next,然后26枚举到最后能放。最后倒序一遍。依然wa17.

然后发现,栈顶之下的元素虽然比栈顶大,但是不一定比后缀大,这边要while不停的判一下栈顶。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout<<#a<<"="<
<
s;int main(void){ cin.tie(0);std::ios::sync_with_stdio(false); cin>>(str+1);LL n=strlen(str+1); memset(suf,0x3f,sizeof(suf)); for(LL i=n;i>=1;i--){ suf[i]=min(suf[i+1],(LL)(str[i]-'a'+1)); } vector
v; for(LL i=1;i<=n;i++){ if(s.empty()){ s.push(str[i]); } else{ while(!s.empty()){ if((s.top()-'a'+1)<=suf[i]){ v.push_back(s.top()); s.pop(); } else break; } s.push(str[i]); } } while(!s.empty()){ v.push_back(s.top());s.pop(); } for(auto i:v){ cout<

 

转载地址:http://eict.baihongyu.com/

你可能感兴趣的文章
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>