题目链接

题目难度: 1700rating

题目类型:string+贪心+STL

题目思路:

由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在,就先把后面的所有的该字符放在答案字符串u中(u可以用queue来表示),而字符串t可以用stack来表示。
中间的字符串都加入到stack中,当一个字符全加入后,继续从小到大的查找剩余字符串中还剩的字符,并且与栈顶元素进行比较,如果小于栈顶元素,需要先输出栈顶元素。
中间的细节比较多,详细看代码。
我用的map来维护剩余的字符数量。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <stack>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
/*** TEMPLATE CODE STARTS HERE ***/
char s[maxn];
int n;
map<char ,int > m;
int main()
{
scanf("%s",s);
n=strlen(s);
rep(i,,n)
{
int x=m[(s[i])];
m[(s[i])] =x+;
}
stack<char> st;
queue<char> q;
while(!q.empty())
{
q.pop();
}
while(!st.empty())
{
st.pop();
}
char x='a'-;
int index=;
while(q.size()!=n)
{
char i;
for( i='a';i<='z';i++)
{
if(m.count(i)!=&&m[i]!=)
{
x=i; break;
}
}
if(i=='z'+)
{
if(!st.empty())
x=st.top();
}
if(!st.empty()&&st.top()<x)
{
x=st.top();
} while(!st.empty()&&st.top()==x)
{
st.pop();
q.push(x);
if(!st.empty()&&st.top()<x)
{
x=st.top();
}
}
if(m.count(x)==||m[x]==)
continue;
int num=m[x];
m.erase(x);
for(int j=index;j<n;j++)
{
if(s[j]==x)
{
q.push(x);
num--;
}else
{
st.push(s[j]);
m[s[j]]=m[s[j]]-;
}
if(!num)
{
index=j+;
break;
}
} }
while(!q.empty())
{
printf("%c",q.front());
q.pop();
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Minimal string CodeForces – 797C的更多相关文章

  1. Minimal string CodeForces - 797C

    Minimal string CodeForces - 797C 题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的 ...

  2. CodeForce-797C Minimal string(贪心模拟)

    Minimal string CodeForces - 797C Petya 收到一个长度不超过 105 的字符串 s.他拿了两个额外的空字符串 t 和 u 并决定玩一个游戏.这个游戏有两种合法操作: ...

  3. Codeforces 797C - Minimal string

    C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...

  4. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  5. 【codeforces 797C】Minimal string

    [题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...

  6. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...

  7. Minimal string 栈 贪心

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

    补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...

  9. Check the string CodeForces - 960A

    A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...

随机推荐

  1. 网络互联技术(2)——前篇—【转载】电脑结构和CPU、内存、硬盘三者之间的关系

    原文链接:传送门 详细内容: 电脑结构和CPU.内存.硬盘三者之间的关系 前面提到了,电脑之父——冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器. 我们看一下现在我们电脑 ...

  2. emacs 配置.emacs

    emacs 配置.emacs (require 'package) (package-initialize) (add-to-list'package-archives '("melpa&q ...

  3. [Hive_add_5] Hive 的 join 操作

    0. 说明 在 Hive 中进行 join 操作 1. 操作步骤 1.0 建表 在 hiveserver2 服务启动的前提下,在 Beeline客户端中输入以下命令 # 新建顾客表 create ta ...

  4. 03.Python网络爬虫第一弹《Python网络爬虫相关基础概念》

    爬虫介绍 引入 之前在授课过程中,好多同学都问过我这样的一个问题:为什么要学习爬虫,学习爬虫能够为我们以后的发展带来那些好处?其实学习爬虫的原因和为我们以后发展带来的好处都是显而易见的,无论是从实际的 ...

  5. 【Teradata】grouping和rollup窗口函数

    1.group by后带rollup子句 先按一定的规则产生多种分组,然后返回各个分组所产生的结果集的并集,且没有去掉重复数据(统计出的数据是求和还是最大值还是平均值等这就取决于SELECT后的聚合函 ...

  6. Why do Kafka consumers connect to zookeeper, and producers get metadata from brokers?

    Why do Kafka consumers connect to zookeeper, and producers get metadata from brokers? Ask Question u ...

  7. 数以亿计运行PHP的网站即将面临严重的安全风险

    数以亿计运行PHP的网站即将面临严重的安全风险 根据W3Techs的统计数据,目前所有互联网站点中约有78.9%使用PHP运行.但是2018年12月31日,PHP 5.6.x的安全支持将正式停止,标志 ...

  8. 测试一下robotgo自动化操作,顺便解决了原来的mingw版本中只有gcc,没有g++的问题

    参考:https://gitee.com/veni0/robotgo#examples  但是编译不成功 找到这个:https://gitee.com/veni0/robotgo#examples ( ...

  9. Difference between BeanFactory and FactoryBean in Spring Framework (Spring BeanFactory与Factory区别)

    参见原文:http://www.geekabyte.io/2014/11/difference-between-beanfactory-and.html geekAbyte Codes and Ran ...

  10. 008_python内置语法

    一. 参考:http://www.runoob.com/python/python-built-in-functions.html (1)vars() 描述 vars() 函数返回对象object的属 ...