Minimal string CodeForces – 797C
题目难度: 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的更多相关文章
- Minimal string CodeForces - 797C
Minimal string CodeForces - 797C 题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的 ...
- 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 ...
随机推荐
- TNS-12535: TNS:operation timed out
AWS数据库云服务器出现了连接超时的错误,于是查看相关时段的alert日志,发现了如下的错误: **************************************************** ...
- SQL Server Browser探究
一.官网关于SQL SERVER Browser服务的解释(谷歌翻译后稍作修改的): https://docs.microsoft.com/en-us/sql/tools/configuration- ...
- CSipIm断网重连崩溃原因分析
断网重连之后的操作流程 拨打电话 ->调用SipService.makeCallOptions(),先重启一遍SipService保证PjSipService的各参数正常,尤其是create参数 ...
- Java反射机制小结和实际操作
一.什么是反射 1.编译和运行 在了解什么是Java反射机制前,先聊聊Java的编译和运行. 还记得第一次使用记事本编写第一个Java程序的场景吗?通过命令窗口,使用javac命令编译一个.java文 ...
- 【PS技巧】常用概念和功能操作
常用概念 1.画布大小与图像大小 画布大小是图像背景的大小,即画纸.图像大小是当前编辑的图层的所有对象大小,即画纸上的画. 常用功能操作 1.打开和新建功能 打开图片:Ctrl+O或双击工作区 图片垂 ...
- Zookeeper的一致性
转载:http://flyfoxs.iteye.com/blog/2121560 下面内容主要摘抄于<<Hadoop实战>>,红色高亮部分是本人添加的白话注释. Zookeep ...
- Java之word导出下载
访问我的博客 前言 最近遇到项目需求需要将数据库中的部分数据导出到 word 中,具体是在一个新闻列表中将选中的新闻导出到一个 word 中.参考了网上一些教程,实现了该功能,在此记录下来. 导出结果 ...
- Linux系统安装和网络配置
系统下载 CentOS 6.x 50% 6.9 ---- 常用 CentOS 7.x 50% 7.2 ----常用 官网-国外 https://wiki.centos.org/Downloa ...
- jira8.0.2安装与破解
环境: centos7.4 java1.8 jira版本:8.0.2 mysql :mariadb 软件下载: 破解包: atlassian-extras-3.2.jar 连接mysql驱动: mys ...
- ganache与metamask
1.其实ganache其实就相当于一个私有链ganache安装,这个是图形化界面的: 2.(testRpc跟他其实是一个用处,有一个即可,只不过testRpc是非图形化界面.要注意两者都仅运行在內存中 ...