[CodeForces - 197C] C - Lexicographically Maximum Subsequence
C - Lexicographically Maximum Subsequence
You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.
We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 < p2 < ... < pk ≤ |s|) a subsequence of string s = s1s2... s|s|.
String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ... , x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. Characters in lines are compared like their ASCII codes.
Input
The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.
Output
Print the lexicographically maximum subsequence of string s.
Example
ababba
bbba
abbcbccacbbcbaaba
cccccbba
Note
Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).
The first sample: aBaBBA
The second sample: abbCbCCaCbbCBaaBA
这个题目就是要求出子串中字典序最大的.
由于本题有许多方法,那我就讲一讲我自己的好了.
由于我们要尽量挑字典序最大的字符作为开头(首关键字并不是长度!),所以,我们要记录当前字符串的最大字符,然后,如果剩下的字符里面,已经不存在这个最大的字符,那么这个字符就下调,知道存在.
这样一个纯模拟的过程就好了,不过不知道为什么我在比赛中时间用的稍微有点多......
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
],nxt[],R[];
],c;
int read(){
,f=; char ch=getchar();
'){if (ch=='-') f=-f; ch=getchar();}
+ch-',ch=getchar();
return x*f;
}
int main(){
scanf("%s",a),len=strlen(a),c='a';
memset(las,,sizeof las);
memset(nxt,,sizeof nxt);
; i>=; i--) ) las[a[i]-'a']=i,nxt[i]=i,R[a[i]-'a']=i;
else nxt[i]=las[a[i]-'a'],las[a[i]-'a']=i;
; i<len; i++) if (c<a[i]) c=a[i];
; i<len; i++) if (a[i]==c){
putchar(c); if (nxt[i]!=i) continue;
);
}
;
}
[CodeForces - 197C] C - Lexicographically Maximum Subsequence的更多相关文章
- Educational Codeforces Round 32 E. Maximum Subsequence
题目链接 题意:给你两个数n,m,和一个大小为n的数组. 让你在数组找一些数使得这些数的和模m最大. 解法:考虑 dfs但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往 ...
- Codeforces 888E Maximum Subsequence
原题传送门 E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
- Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value 贪心
题意:E.Maximum Subsequence Value 题意: 给你n 个元素,你挑选k个元素,那么这个 k 集合的值为 ∑2i,其中,若集合内至少有 max(1,k−2)个数二进制下第 i 位 ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- Maximum Subsequence Sum(接上篇)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- Javascript 高级程序设计(第3版) - 第01章
2017-05-10 js简介 一个叫“不难登”的人发明的.js的流行是因为 ajax 的关系. js分为三个部分: 核心: ECMAScript 文档对象模型: DOM 浏览器对象模型: BOM 核 ...
- BZOJ 1040: [ZJOI2008]骑士(基环树dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题意: 思路: 这是基环树,因为每个人只会有一个厌恶的人,所以每个节点只会有一个父亲节点,但是 ...
- HDU 4312 Meeting point-2(切比雪夫距离转曼哈顿距离)
http://acm.hdu.edu.cn/showproblem.php?pid=4312 题意:在上一题的基础上,由四个方向改为了八个方向. 思路: 引用自http://blog.csdn.net ...
- JavaScript的案例(数据校验,js轮播图,页面定时弹窗)
1.数据校验 步骤 1.确定事件(onsubmit)并绑定一个函数 2.书写这个函数,获取数据,并绑定id 3. ...
- ashx和aspx的区别
1. ashx是一般处理程序,一般返回的数据有两种,一种是html页面,一种是只返回一个字符串. 2. aspx是web窗体程序,每次新建都回自带一个界面和一个后台处理程序. 3. 根据以上两点,可以 ...
- <script src="../build/browser.min.js"></script> 是用来里面babel工具把ES6的语法转成ES5
<!DOCTYPE html> <html> <head> <script src="../build/react.js">< ...
- Eclipse启动Web项目 Tomcat中webapps中没有项目文件夹
原文出处:https://blog.csdn.net/JYH1314/article/details/51656233 1.eclipse不像MyEclipse默认将项目部署到tomcat安装目录下的 ...
- newborn, infant, toddler以及baby的区别
1.An infant (from the Latin word infans, meaning "unable to speak" or "speechless&quo ...
- JS 字符串两边截取空白的trim()方法的封装
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [MySQL]典型的行列转换
列变成行 测试数据库数据样式: 应用的sql语句: SELECT TM,NAME,SUM(GE) AS 'GE',SUM(GD) AS 'GD',SUM(CT) AS 'CT',SUM(NUM) AS ...