【codeforces 797C】Minimal string
【题目链接】:http://codeforces.com/contest/797/problem/C
【题意】
一开始,给你一个字符串s;两个空字符串t和u;
你有两种合法操作;
1.将s的开头字符加到t后面;
2.将t的最后一个字符加到u的后面去
要求最后使得s和t字符串变成空串;
并且得到的u的字符串的字典序最小;
【题解】
i层循环顺序枚举s字符串的每一个字符;
然后把这第i个字符s[i]加入到t的后面去->用一个栈来模拟;
然后维护栈顶的元素和s中剩下的字符串里面字典序最小的字母;->O(26)得到;设为now;
如果栈顶元素为now;
就把栈顶元素弹到答案字符串后面去;
然后把新的栈顶元素再考虑进去,然后再求字典序最小的字母;
重复上述步骤直至字典序最小的字母不为栈顶元素为止;
这样做就是贪心地保证从最高位开始每一位的字典序都是最小的;
【Number Of WA】
3
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
int bo[300],len;
char s[N],now;
stack <char> sta;
string ans;
char get_now()
{
for (char t = 'a';t <='z';t++)
if (bo[t])
return t;
return '%';
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
ans="";
cin >> (s+1);
len = strlen(s+1);
rep1(i,1,len)
bo[s[i]]++;
rep1(i,1,len)
{
now = get_now();
sta.push(s[i]);
while (!sta.empty() && sta.top()==now)
{
//assert(sta.top()==now);
ans+=now;
bo[now]--;
sta.pop();
if (!sta.empty())
bo[sta.top()]++;
now = get_now();
}
if (!sta.empty())
bo[sta.top()]--;
}
while (!sta.empty()) ans+=sta.top(),sta.pop();
cout << ans << endl;
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 797C】Minimal string的更多相关文章
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 779D】String Game
[题目链接]:http://codeforces.com/contest/779/problem/D [题意] 给你一段操作序列; 按顺序依次删掉字符串1中相应位置的字符; 问你最多能按顺序删掉多少个 ...
- 【codeforces 801B】Valued Keys
[题目链接]:http://codeforces.com/contest/801/problem/B [题意] 定义一个对两个字符串x,y的f(x,y)函数; 返回的是一个字符串; 这个返回的字符串的 ...
- 【codeforces 801A】Vicious Keyboard
[题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...
- 【codeforces 510C】Fox And Names
[题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...
- 【codeforces 514A】Chewbaсca and Number
[题目链接]:http://codeforces.com/contest/514/problem/A [题意] 允许你把每个数字翻转 ->x变成9-x 然后问你能够变成的最小的数字是什么; 不能 ...
- 【codeforces 514C】Watto and Mechanism(字典树做法)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
随机推荐
- ubuntu安装go语言
1.下载安装包 2.解压 sudo tar -zvxf go1.10.linux-amd64.tar.gz -C /usr/local 3.配置 sudo vim /etc/profile 添加 #s ...
- eclipse和jdk版本对应问题
日常开发中,32位eclipse要用32位jdk,64位则必须要用64位jdk,否则启动时就会报错,load jvm.dll失败,昨天又遇到了这个问题.更换对应的版本之后就好了.tomcat等应用也有 ...
- IDEA Spark Streaming 操作(套接字流)-----make socket数据源
import java.io.PrintWriter import java.net.ServerSocket import scala.io.Source object DStream_makeSo ...
- Istio 1.1部署实践
前提条件 正确安装配置Kubernetes集群 CentOS Linux release 7.5.1804 安装 下载istio 1.1版本 [root@vm157 ~]# wget https:// ...
- vue中时间控件绑定多个输入框
首先去下载laydate时间控件,引入到相应的模板中 <input type="text" val-required="" value="&qu ...
- js返回上一层
Javascript 返回上一页 1. Javascript 返回上一页 history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). wind ...
- html 基础演示代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 安装cloudermanager时如何正确Configuring TLS Security for Cloudera Manager
不多说,直接上干货! 参考官网 https://www.cloudera.com/documentation/enterprise/5-2-x/topics/cm_sg_config_tls_secu ...
- Win10中的睡眠、休眠
共同点: 都是节能技术. 异同点: 睡眠: 需要耗电.通过键盘鼠标唤醒.唤醒速度快.将用户正在处理的数据保存到内存中,除内存以外的所有设备都停止供电. 休眠: 不需耗电.通过电源键唤醒.唤醒速度慢.将 ...
- SAS进阶《深入解析SAS》之SAS数据挖掘的一般流程
SAS进阶<深入解析SAS>之SAS数据挖掘的一般流程 1. 所谓数据挖掘,是指通过对大量的数据进行选择.探索与建模,来揭示包含在数据中以前不为人所知的模式或规律,从而为商业活动或科学研究 ...