codeforce 724D Dense Subsequence (字符串||贪心好题)
来源:互联网 发布:suse12网络配置端口 编辑:程序博客网 时间:2024/05/21 18:38
You are given a string s, consisting of lowercase English letters, and the integerm.
One should choose some symbols from the given string so that any contiguous subsegment of lengthm 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 that1 ≤ 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 ak 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 stringsip1sip2...sipt.
Find the lexicographically smallest string, that can be obtained using this procedure.
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 exceed100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
3cbabc
a
2abcab
aab
3bcabcbaccba
aaabb
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".
贪心,先判断只由'a'-c的字符组成该字符串是否可行,如果可行,那么我们肯定是拿'a'越多越好,拿‘b’……c-1也是越多越好(aaabbXX肯定比aaabXXX字典序小),也就c字符需要特判一下,因为c字符不是越多越好,而是越少越好,只有在满足前面的越多越好的前提下,不得不拿c才拿
解题思路:aaabbXX肯定比aaabXXX字典序小,找到最大的字母(肯定要出现的那个)ch,比ch小的字母肯定要全出现才能使得sort字典序最小,
难点在于,最少选出几个ch,才能满足要求,这里就要用到贪心,m=4,ccbbccc,第三个跟第四个选,要做判断,很明显,当前状况都满足,我们选
尽量靠后的加入answer,因为可以少贡献字母。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>using namespace std;const int N = 1e5+100;char s[N];map<char,int>num;int main(){int m,i,j,cnt,ans;scanf("%d%s",&m,s+1);int len=strlen(s+1);char ch;for(ch='a';ch<='z';ch++) { //选最大的字母 cnt=0;for(i=1;i<=len;i++) {cnt++;if(s[i]<=ch) cnt=0;else if(cnt==m) break;}if(i>len) break;}cnt=0;for(i=1;i<=len;i++) { //最大的字母最少出现几次才能满足任意连续长为m的字符串 cnt++;if(s[i]<ch) {num[s[i]]++;cnt=0;}else if(s[i]==ch) {for(j=1;j<=m-cnt&&i+j<=len;j++) {if(s[i+j]<=ch) break;}if(j>m-cnt) {num[ch]++;cnt=0;}}}for(char ch1='a';ch1<=ch;ch1++) {while(num[ch1]--) printf("%c",ch1);}printf("\n");return 0;}
- codeforce 724D Dense Subsequence (字符串||贪心好题)
- CodeForce 724D. Dense Subsequence bY Assassin
- 【Codeforces 724D】 Dense Subsequence【贪心】
- Codeforces 724D Dense Subsequence(贪心)
- 【Codeforces 724 D Dense Subsequence】+ 贪心
- 【29.41%】【codeforces 724D】Dense Subsequence
- 【codeforces 724D】【贪心】 Dense Subsequence 【一个字符串,按照一定的区间要求从中选出一些字符,使得这串字符的sort后字典序最小】
- 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表+贪心
- codeforce 754 D k个区间的交的尽可能的大 (优先队列) 贪心好题!!!
- Codeforce 798D(贪心)
- Intel Code Challenge Final Round D Dense Subsequence(线段树)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- D. Dense Subsequence (技巧枚举)
- Codeforce 705D 贪心选择
- Codeforces Round Intel Code Challenge Final Round D. Dense Subsequence
- codeforce 3D. Least Cost Bracket Sequence(贪心)
- codeforce 55 D. Beautiful numbers(数位dp,好题)
- codeforce 570D Tree Requests(dfs+位运算,好题)
- QT数据库--添加MySQL驱动插件(Linux篇)
- linux环境下安装curses库(包含库文档安装)
- qt配置 QSetting使用方法
- 【C语言】判断一个字符串是否为另一个字符串的回旋字符串
- QT数据库--添加MySQL驱动插件(mingwin32)
- codeforce 724D Dense Subsequence (字符串||贪心好题)
- ASUS EZ Installer v1.02.11
- iOS开发-模仿网易新闻首页联合滚动动画
- QT数据库--添加ODBC驱动插件(Windows、Linux、ARM篇)
- 381.Spiral Matrix II-螺旋矩阵 II(中等题)
- 【hdu2462】【数论】【欧拉函数+欧拉定理+大数快速幂】The Luckiest number
- C#WinForm中复制、粘贴文本到剪贴板
- 最简单的遗传算法
- c++初学之运算符重载