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.

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 exceed100 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
3cbabc
Output
a
Input
2abcab
Output
aab
Input
3bcabcbaccba
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".

给你一个字符串,让你从中选出一串字符,使得这串字符的sort后字典序最小,选字符的要求是如果上一个选的是i 那么[i+1,i+m]中至少选一个,[1,m]中必须选一个


贪心,先判断只由'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;} 




0 0
原创粉丝点击