【题目链接】:http://codeforces.com/contest/797/problem/C

【题意】



一开始,给你一个字符串s;两个空字符串t和u;

你有两种合法操作;

1.将s的开头字符加到t后面;

2.将t的最后一个字符加到u的后面去

要求最后使得s和t字符串变成空串;

并且得到的u的字符串的字典序最小;

【题解】



i层循环顺序枚举s字符串的每一个字符;

然后把这第i个字符s[i]加入到t的后面去->用一个栈来模拟;

然后维护栈顶的元素和s中剩下的字符串里面字典序最小的字母;->O(26)得到;设为now;

如果栈顶元素为now;

就把栈顶元素弹到答案字符串后面去;

然后把新的栈顶元素再考虑进去,然后再求字典序最小的字母;

重复上述步骤直至字典序最小的字母不为栈顶元素为止;

这样做就是贪心地保证从最高位开始每一位的字典序都是最小的;



【Number Of WA】



3



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; int bo[300],len;
char s[N],now;
stack <char> sta;
string ans; char get_now()
{
for (char t = 'a';t <='z';t++)
if (bo[t])
return t;
return '%';
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
ans="";
cin >> (s+1);
len = strlen(s+1);
rep1(i,1,len)
bo[s[i]]++;
rep1(i,1,len)
{
now = get_now();
sta.push(s[i]);
while (!sta.empty() && sta.top()==now)
{
//assert(sta.top()==now);
ans+=now;
bo[now]--;
sta.pop();
if (!sta.empty())
bo[sta.top()]++;
now = get_now();
}
if (!sta.empty())
bo[sta.top()]--;
}
while (!sta.empty()) ans+=sta.top(),sta.pop();
cout << ans << endl;
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 797C】Minimal string的更多相关文章

  1. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  4. 【codeforces 779D】String Game

    [题目链接]:http://codeforces.com/contest/779/problem/D [题意] 给你一段操作序列; 按顺序依次删掉字符串1中相应位置的字符; 问你最多能按顺序删掉多少个 ...

  5. 【codeforces 801B】Valued Keys

    [题目链接]:http://codeforces.com/contest/801/problem/B [题意] 定义一个对两个字符串x,y的f(x,y)函数; 返回的是一个字符串; 这个返回的字符串的 ...

  6. 【codeforces 801A】Vicious Keyboard

    [题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...

  7. 【codeforces 510C】Fox And Names

    [题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字 ...

  8. 【codeforces 514A】Chewbaсca and Number

    [题目链接]:http://codeforces.com/contest/514/problem/A [题意] 允许你把每个数字翻转 ->x变成9-x 然后问你能够变成的最小的数字是什么; 不能 ...

  9. 【codeforces 514C】Watto and Mechanism(字典树做法)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

随机推荐

  1. ALSA声卡驱动中的DAPM详解之五:建立widget之间的连接关系

    前面我们主要着重于codec.platform.machine驱动程序中如何使用和建立dapm所需要的widget,route,这些是音频驱动开发人员必须要了解的内容,经过前几章的介绍,我们应该知道如 ...

  2. MSP430:PWM产生

    #define     PWM                      BIT6 //  Description: This program generates one PWM output on ...

  3. Swagger 教程

    转自   Vojtech Ruzicka的编程博客 (一)Swagger和SpringFox 记录REST API非常重要.它是一个公共接口,其他模块,应用程序或开发人员可以使用它.即使你没有公开曝光 ...

  4. Python多线程、多进程

    1.from  multiprocessing import Process ;  from  threading import Thread 2.进程之间的数据传输 ,一般会使用到pipes, qu ...

  5. 334 Increasing Triplet Subsequence 递增的三元子序列

    给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下:    如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,    ...

  6. Android进入一个新页面,EditText失去焦点并禁止弹出键盘

    android在进入一个新页面后,edittext会自动获取焦点并弹出软键盘,这样并不符合用户操作习惯. 在其父控件下,添加如下的属性,就可以完美解决,使其进入页面后不主动获取焦点,并且不弹出软键盘: ...

  7. 【技术累积】【点】【java】【29】MapUtils

    内容 是Apache组织下的commons-collections包中的工具类 <dependency> <groupId>commons-collections</gr ...

  8. ICMP,ARP协议

    ICMP ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制 ...

  9. C# 获得星期几

    var temp = System.DateTime.Today.ToString("dddd", new System.Globalization.CultureInfo(&qu ...

  10. [HNOI2004]宠物收养所 题解

    一杯茶,一包烟,一道水题调一天 题面 这题一眼看上去就是个裸板子对吧 本来以为要两棵splay,读了一下题发现店里只能有一种生物(人/宠物) 所以记录一下当前店里的状态就行了 老年手速20min过编译 ...