Minimal string CodeForces - 797C
Minimal string CodeForces - 797C
题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的最后。要求使得最后s和t均为空串。求字典序最小的可能得到的u。
分析:这道题的操作相当于“将s中字符按从左到右顺序入栈,在任意次的入栈后可以进行任意次的出栈”。显然,最后得到字符串的长度一定是相等的,因此字典序最小就是要前面的尽可能的小。因此可以用一个数组分别记录还未入栈的'a'到'z'的个数,按照'a'到'z'的顺序去找。每找一个字符c的第一步是:首先在栈顶找出所有大于等于c的字符,出栈并输出。之后的操作只有在还有未入栈的c时才去做:在原串中还未入栈的部分从前往后找c,把其他字符入栈,把c直接输出(相当于入栈后直接出栈、输出),直到所有未入栈的c没有了。找完'z'后,再将所有栈中剩余字符出栈并输出即可。
#include<cstdio>
#include<cstring>
char s[];
int a[],now,top;
char st[];
//曾经误将top写成char类型导致re
int main()
{
scanf("%s",s);
int i,len=strlen(s);
for(i=;i<len;i++)
a[s[i]]++;
for(i='a';i<='z';i++)
{
while(top>&&st[top]<=i)
{
printf("%c",st[top]);
top--;
}
while(a[i])
{
while(s[now]!=i&&now<len)
{
st[++top]=s[now];
a[s[now]]--;
now++;
}
while(s[now]==i&&now<len)
{
printf("%c",i);
a[i]--;
now++;
}
}
}
while(top>)
{
printf("%c",st[top--]);
}
return ;
}
Minimal string CodeForces - 797C的更多相关文章
- Minimal string CodeForces – 797C
题目链接 题目难度: 1700rating 题目类型:string+贪心+STL 题目思路: 由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在 ...
- CodeForce-797C Minimal string(贪心模拟)
Minimal string CodeForces - 797C Petya 收到一个长度不超过 105 的字符串 s.他拿了两个额外的空字符串 t 和 u 并决定玩一个游戏.这个游戏有两种合法操作: ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- 【codeforces 797C】Minimal string
[题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...
- D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...
- Minimal string 栈 贪心
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]
补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...
- Check the string CodeForces - 960A
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...
随机推荐
- notpad++快捷键
Notpad++快捷键 Notepad++选中行操作 快捷键 使用技巧 作者: Rememberautumn 分类: 其他 发布时间: 2014-09-04 14:18 阅读: 60,106 微信小 ...
- curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused
今天我用curl命令,无论如何都是出现: curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused 找了很久,不知道 ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- contents属性
CALayer 有一个属性叫做contents,这个属性的类型被定义为id,意味着它可以是任何类型的对象.在这种情况下,你可以给contents属性赋任何值,你的app仍然能够编译通过.但是,在实践中 ...
- codeforces 437A. The Child and Homework 解题报告
题目链接:http://codeforces.com/problemset/problem/437/A 题目意思:给出四个选项A.B.C.D选项的内容描述,要求选出符合以下条件的一项. (1)如果某个 ...
- STL Algorithms 之 unique
C++的文档中说,STL中的unique是类似于这样实现的: template <class ForwardIterator> ForwardIterator unique ( Forwa ...
- Android中android:visibility的3中属性的剖析
在Android中控件或者布局的可见性android:visibility有3中情况,他们分别是: View.VISIBLE,View.UNVISIBLE,View.GONE View.VISIBLE ...
- [Selenium] 使用Firefox Driver 示例
//导入Selenium 库和FirefoxDriver 库 package com.learningselenium.simplewebdriver; import org.openqa.selen ...
- [Selenium] 测试机器上安装了多个Firefox,如何指定运行哪一个?
可通过FirefoxBinary 来指定运行某个路径下的Firefox, 示例代码如下: public class testFirefoxBinary{ public static void main ...
- Servlet3.0之九:web模块化
一.使用web-fragment.xml 在Servlet 3.0中,可以使用标注来设置Servlet的相关信息.实际上,Web容器并不仅读取/WEB-INF/classes中的Servlet标注信息 ...