【Codeforces 724 D Dense Subsequence】+ 贪心
来源:互联网 发布:淘宝首页全屏怎么装修 编辑:程序博客网 时间:2024/06/08 17:39
D. Dense Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 ≤ i1 < i2 < … < it ≤ |s|. The selected sequence must meet the following condition: for every j such that 1 ≤ j ≤ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, j + m - 1], i.e. there should exist a k from 1 to t, such that j ≤ ik ≤ j + m - 1.
Then we take any permutation p of the selected indices and form a new string sip1sip2… sipt.
Find the lexicographically smallest string, that can be obtained using this procedure.
Input
The first line of the input contains a single integer m (1 ≤ m ≤ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn’t exceed 100 000. It is also guaranteed that the number m doesn’t exceed the length of the string s.
Output
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
Examples
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
Note
In the first sample, one can choose the subsequence {3} and form a string “a”.
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are ‘a’, ‘b’ and ‘a’) and rearrange the chosen symbols to form a string “aab”.
贪心,在[l + 1, l +m-1]里选出最小,字典序最下,把比选中的最大的字符小的都加上?
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int K = 1e5 + 10;bool vis[K];char s[K],st[K],ss ='a';int main(){ int N; scanf("%d %s",&N,s); int nl = strlen(s),pl = 0,kl = 0; while(pl + N - 1 < nl){ int ml = pl; for(int i = pl; i <= pl + N - 1; i++) if(s[i] <= s[ml]) ml = i; st[++kl] = s[ml],ss = max(ss,s[ml]),pl = ml + 1,vis[ml] = 1; } for(int i = 0 ; i < nl ; i++) if(!vis[i] && s[i] < ss) st[++kl] = s[i]; sort(st + 1, st + 1 + kl); printf("%s\n",st + 1); return 0;}
- 【Codeforces 724D】 Dense Subsequence【贪心】
- Codeforces 724D Dense Subsequence(贪心)
- 【Codeforces 724 D Dense Subsequence】+ 贪心
- 【29.41%】【codeforces 724D】Dense Subsequence
- codeforce 724D Dense Subsequence (字符串||贪心好题)
- 【codeforces 724D】【贪心】 Dense Subsequence 【一个字符串,按照一定的区间要求从中选出一些字符,使得这串字符的sort后字典序最小】
- CodeForce 724D. Dense Subsequence bY Assassin
- Codeforces Round Intel Code Challenge Final Round D. Dense Subsequence
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 贪心 + RMQ
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence ST表+贪心
- Codeforces-632D Longest Subsequence
- Codeforces 632D Longest Subsequence
- CodeForces 545D贪心
- codeforces 276D 贪心
- CodeForces-748D 贪心
- Codeforces 758D 贪心
- Codeforces 767D 贪心
- Codeforces 853D 贪心
- Effective Java读书笔记六:方法
- 从jvm的角度来看java的多线程
- 题目1085:求root(N, k)
- Some day some time we will do
- 十八条微信官方关于小程序的精选问答都在这了
- 【Codeforces 724 D Dense Subsequence】+ 贪心
- 1081. Rational Sum (20)
- Mac与Phy组成原理的简单分析
- java基础之网络编程
- express的router.js源码分析
- HDU 2045 不容易系列之(3)—— LELE的RPG难题 【递推】
- 早该开始了,现在正好,与bug斗,与坑斗,其乐无穷!
- 1008: FBI树
- Const Buffer,Texture Buffer