CodeForce 724D. Dense Subsequence bY Assassin

D. Dense Subsequence
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 100000).
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

这题超级简单你敢信?就是个贪心。。。每次从头开始,然后每次从m个中选出最小的,下一次从上次的最小开始,直到结束,每次记录一下次数,吧用到的最大的字母挑出来,小于他的原字符串有多少输出多少,最大的那个用到多少输出多少。。。真蠢这题错了10次?还不会看了别人的代码。。。

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
char a[100010];
int c[26];
int use[26];
int m;
int main()
{
    input;
    int i,j;
    string s;
    while(scanf("%d",&m)!=EOF)
    {
        memset(c,0,sizeof(c));
        memset(use,0,sizeof(use));
        cin>>s;
        for(i=0;i<s.length();i++)
        {
            a[i+1]=s[i];
            c[s[i]-'a']++;
        }
        int len=s.size();
        int pos=0,p,maxx,minn;
        for(maxx=0,pos=0;pos+m<=len;)
        {
            minn=26;
            p=pos;
            for(i=1;i<=m;i++)
            {
                if(a[pos+i]-'a'<=minn)
                {
                    minn=a[pos+i]-'a';
                    p=i+pos;
                }
            }
            use[a[p]-'a']++;
            if(maxx<a[p]-'a')
            {
                maxx=a[p]-'a';
            }
            pos=p;
        }
        for(i=0;i<maxx;i++)
            for(j=1;j<=c[i];j++)
                printf("%c",i+'a');
        for(i=1;i<=use[maxx];i++)printf("%c",maxx+'a');
        cout<<endl;
    } 
    return 0;
 } 

膜拜zchahaha

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值