【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个字符串 对于每一个询问字符串 你需要在 ...
随机推荐
- Python:SMOTE算法——样本不均衡时候生成新样本的算法
Python:SMOTE算法 直接用python的库, imbalanced-learn imbalanced-learn is a python package offering a number ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- C. Searching for Graph(cf)
C. Searching for Graph time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Graphics.DrawMeshInstanced
Draw the same mesh multiple times using GPU instancing. 可以免去创建和管理gameObj的开销 并不是立即绘制,如需:Graphics.Draw ...
- 使用内存流导出Excel
public MemoryStream MemoryStreamDeiveFlowInfoaging(DataTable Table) { Dictionary<int , string> ...
- IDEA报错,注解标红,提示Cannot resolve symbol xxx
一般都是jar包没导进来,可以先看一下setting里maven配置的路径对不对
- Java初级进阶中高级工程师必备技能
很多人学了javase以为自己学的已经很OK了,但是其实javase里边有很多的知识点是你不知道的,不管你找的是哪里的javase的视频,大多数是不会讲这些东西,而这些东西你平时业务又不会主动去接触, ...
- C# System.Environment.GetFolderPath的使用 [转]
原文:https://blog.csdn.net/yongyong521/article/details/75105853 获取系统文件目录 string strPath = Environment. ...
- 315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数
给定一个整型数组 nums,按要求返回一个新的 counts 数组.数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于nums[i] 的元素的数量.例子:给定 nu ...
- Python3之切片的道理
list的切片有三个参数:起点,终点,步长 list[::-1] 相当于起点为最后的一个,终点为第一个,然后一次减少一个 更多的看下面的测试 >>> a = [0,1,2,3,4,5 ...