Educational Codeforces Round 19 C. Minimal string
来源:互联网 发布:音频信号发生器软件 编辑:程序博客网 时间:2024/06/11 03:27
题目大意:给一个字符串s , 和两个空串t, u;
两种操作:
1)提出s的首字母添加到t字符串的尾部
2)提出t字符串的尾部字母添加到u字符串的尾部
当s, t 为空后,所能得到的字典序最小的字符串u;
题目分析:
记录每个字母出现的次数
对s字符串进行遍历,对当前遍历的位置i, 得到字母s[i] 加入到stack栈中, 根据输入的字母判断 (i+1)到结束 是否存在字母x < 栈首字母y;
如果没有,出栈。 反之继续进行遍历;
具体细节参见代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = (int)1e5+5;int top, ch[26];char s[maxn], Stack[maxn], u[maxn]; // u字符串存储最后的答案;bool calc(char u){ // false 表示没有找到; int v = u-'a'; for(int i = 0; i < v; ++i){ if(ch[i] != 0) return true; } return false;}int main(){ scanf("%s", s); int len = strlen(s); for(int i = 0; i < len; ++i) ch[s[i]-'a']++; top = 0; int r = 0; // r表示 字符串u当前的长度; for(int i = 0; i < len; ++i){ ch[s[i]-'a']--; Stack[top++] = s[i]; while(top > 0 && calc(Stack[top-1])==false){ u[r++] = Stack[--top]; } } u[r++] = '\0'; printf("%s", u);}
0 0
- Educational Codeforces Round 19 C. Minimal string
- Educational Codeforces Round 19 C. Minimal string
- Educational Codeforces Round 25 E. Minimal Labels
- Educational Codeforces Round 25 E. Minimal Labels
- Educational Codeforces Round 19 C题解
- Educational Codeforces Round 9 -- C - The Smallest String Concatenation
- Educational Codeforces Round 9 C The Smallest String Concatenation
- Educational Codeforces Round 9 C. The Smallest String Concatenation【思维】
- Educational Codeforces Round 25 E. Minimal Labels(拓扑排序)
- Educational Codeforces Round 25E. Minimal Labels(拓扑排序+思维)
- [拓扑序] Educational Codeforces Round 25 825E. Minimal Labels
- Educational Codeforces Round 21 C
- Educational Codeforces Round 5 C
- Educational Codeforces Round 26 C
- Codeforces 797C-Minimal string
- CodeForces 797C Minimal string
- codeforces Educational Codeforces Round 16-C
- Educational Codeforces Round 19 题解
- 第一篇
- POJ 1721-CARDS(置换群-单个循环置换幂运算 倒推)
- Pattern与Matcher类
- poj-Catch That Cow(排位赛8-c)
- 0418PHP
- Educational Codeforces Round 19 C. Minimal string
- opencv连续读取文件夹下的图片(python)
- UESTC 1060 秋实大哥与快餐店(字典树)
- 用Condition实现线程间高效通信
- 【BZOJ】3670 [Noi2014]动物园 KMP
- HDU-2017-字符串统计
- BZOJ 2243: [SDOI2011]染色 [树链剖分+细节]【数据结构】
- C++ SIGSEGV check list
- [转]ubuntu vivado hardware manager 找不到板子,要安装JTAG驱动