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 ...
随机推荐
- 通过linkserver不能调远程表值函数
Question: 通过linkserver调远程表值函数报错如下 Solution: 注意:查询语句中的[SDS_NONEDI].[DBO].ddddd(),不能加server名[sdsc2-1]. ...
- SQL Server 一致性读
我们在Oracle和MySQL数据库中已经对一致性读的概念比较熟悉了,但是在SQL Server中却鲜少提及,但SQL Server自2005版本以来其实也实现了一致性读,几乎所有关系型数据库产品的一 ...
- xss挑战之旅wp
Level 1 - 180831 第一关很简单,开胃菜 payload: http://localhost/xss_game/level1.php?name=test123<script&g ...
- linux c 语言之--fseek(),fseeko(),fseeko64()讲解 (转载)
转载:http://blog.csdn.net/lemoncyb/article/details/16841317 fseek() 函数讲解: 函数定义: int fseek(FILE *stream ...
- SSL 原理及 https 配置
目录 1. SSL 原理 1.1. SSL 简介 1.2. 主要概念 1.3. 建立安全连接的过程 2. https 配置 (以 nginx 为例) SSL 原理 SSL 简介 SSL (Secure ...
- 用static声明外部变量与内、外部函数
1.用static声明外部变量 若希望某些外部变量只限于被本文件引用,而不能被其他文件引用,可以在定义外部变量时加一个static声明. 例:(file1.c) #include <stdafx ...
- MySQL 目录结构、配置文件、修改密码
查看全局数据文件路径 show global variables like "%datadir%" 一.文件目录结构 文件安装路径为F:/JJ/MYSQL-5.6.42-WINX6 ...
- vue指令详解和自定义指令
在vue中,指令以v-开头,是一种特殊的自定义行间属性,指令的职责就是其表达式的值改变时相应地将某些行为应用到DOM上 指令使用的示例 在下面的运行结果中可以看到,v-html是可以解析html标签的 ...
- python六十四课——高阶函数练习题(二)
总结:高阶函数以及匿名函数之间的配合使用 from functools import reduce #模块一:lambda和filter的结合使用 #lt = [1,2,3,4,5,6,7,8,9] ...
- CSS--块级元素和行内元素
相同:设置后,对应的模块都会脱离文档流 不同点:position相应的块级元素会覆盖下面的内容(文字,),而float只会覆盖块级元素,里面的文字会脱离 出来 float是浮动定位,position是 ...