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的更多相关文章

  1. Minimal string CodeForces – 797C

    题目链接 题目难度: 1700rating 题目类型:string+贪心+STL 题目思路: 由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在 ...

  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. (转载)解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes

    今天想用用MySQL 数据库  谁知道老提示 1045 access denied for user 'root'@'localhost' using password yes 最后在csdn 上找到 ...

  2. wx.request的并发问题

    wepyjs - 小程序组件化开发框架 https://tencent.github.io/wepy/document.html#/ 在同时并发10个request请求测试时: 不使用WePY: 使用 ...

  3. Pattern: Microservice Architecture

    Microservice Architecture pattern http://microservices.io/patterns/microservices.html Context You ar ...

  4. jws webservice code

    1.服务器端建立 1.1.创建接口 [java] view plaincopy @WebService  public interface IWebService {      int add(int ...

  5. CH 5302 金字塔(区间DP)

    CH 5302 金字塔 \(solution:\) 很神奇的一道题目,当时看到还以为是一道字符串求回文子串的题目.但是数据范围很小,而且只知道回文串也不好做.但是我们观察可得,如果是深度搜索便利,那么 ...

  6. Spring Boot 整合Servlet

    冷知识,几乎用不到 在spring boot中使用Servlet有两种实现方法: 方法一: 正常创建servlet,然后只用注解@ServletComponentScan package clc.us ...

  7. YTU 2455: Pefect 数字

    2455: Pefect 数字 时间限制: 1 Sec  内存限制: 128 MB 提交: 749  解决: 146 题目描述 小明和小林做数字游戏,他们的游戏规则如下: 小明说出一个数字n,小林说出 ...

  8. vue中使用axios post上传头像/图片并实时显示到页面

    在前端开发中,为了更好的用户体验,在头像上传时会先将图片显示到页面然后点击保存按钮 完成图片的上传成功 代码部分有参考他人的写法. html代码:   <div id="myPhoto ...

  9. Docker安装 人生第一次

    Ubuntu 系列安装 Docker 通过系统自带包安装 Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装. $ sudo apt-get update $ sudo a ...

  10. UIButton常见属性和方法

    一.创建,两种方法: 1. 常规的 initWithFrame UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 8 ...