【题目链接】: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的更多相关文章

  1. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  4. 【codeforces 779D】String Game

    [题目链接]:http://codeforces.com/contest/779/problem/D [题意] 给你一段操作序列; 按顺序依次删掉字符串1中相应位置的字符; 问你最多能按顺序删掉多少个 ...

  5. 【codeforces 801B】Valued Keys

    [题目链接]:http://codeforces.com/contest/801/problem/B [题意] 定义一个对两个字符串x,y的f(x,y)函数; 返回的是一个字符串; 这个返回的字符串的 ...

  6. 【codeforces 801A】Vicious Keyboard

    [题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...

  7. 【codeforces 510C】Fox And Names

    [题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...

  8. 【codeforces 514A】Chewbaсca and Number

    [题目链接]:http://codeforces.com/contest/514/problem/A [题意] 允许你把每个数字翻转 ->x变成9-x 然后问你能够变成的最小的数字是什么; 不能 ...

  9. 【codeforces 514C】Watto and Mechanism(字典树做法)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

随机推荐

  1. E: Unable to lock the administration directory (/var/lib/dpkg/)

    如何修复 Ubuntu 中的“Unable to lock the administration directory (/var/lib/dpkg/)” 在 Ubuntu 或者它的衍生版如 Linux ...

  2. What is an ISAPI Extension?

    https://www.codeproject.com/Articles/1432/What-is-an-ISAPI-Extension Introduction Unless you have be ...

  3. vs2010打开vs2012项目

    修改.sln文件的前两行 修改前: Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 修 ...

  4. Vue解决移动端localhost无数据问题

    正常web端调用后台接口时使用localhost或者ip都能访问后台数据,但是在移动端上使用localhost却无法访问后台数据 这时候需要把localhost改成ip就可以在移动端上访问后台数据了

  5. 数据库得到too many connections”错误信息

    查进程  show processlist删除进程  kill ID查完整sql  show full processlist; 连数据库   MySQL -S /tmp/mysql.sock   或 ...

  6. j建立一个小的servlet小程序

    我们建立一个最简单的servlet程序,这个servelt程序只是单纯的输出helloworld. 步骤如下:如图:在Eclipse中选择新建一个项目,其中选择tomcat project然后点击下一 ...

  7. ACM_Alien And Password

    Alien And Password Time Limit: 2000/1000ms (Java/Others) Problem Description: Alien Fred wants to de ...

  8. 334 Increasing Triplet Subsequence 递增的三元子序列

    给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下:    如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,    ...

  9. Kafka的3节点集群详细启动步骤(Zookeeper是外装)

    首先,声明,kafka集群是搭建在hadoop1.hadoop2和hadoop3机器上. kafka_2.10-0.8.1.1.tgz的1或3节点集群的下载.安装和配置(图文详细教程)绝对干货 如下分 ...

  10. C#学习-执行存储过程

    使用存储的优点 1.执行更快.直接写sql脚本会有个解析编译的过程. 2.修改方便.当业务改变时,只需要改存储过程,不需要修改C#代码 3.传递Sql脚本数据相对更小 缺点: 1.使用存储过程,数据库 ...