Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- D. Dense Subsequence (技巧枚举)

大体题意:

给你一个字符串,和一个数m ,要求在这个字符串中选出一些位置,使得任意连续的长度为m 的字符串中至少包括一个这些位置的字符!输出这些字符组成字符串的最小字典序!

思路:

借鉴了学长的博客!

感觉很巧妙:

枚举最后一个字母id!

然后开始扫这个字符串,如果这个字符小于 id  加入到ans里

如果等于id  先用栈记录一下,为什么用栈呢?因为最后用的时候肯定是用最远的一个,栈最后加入的就是最远的!

如果跑了长度为m 了还是没找到 比id 小的,就处理 等于id的 。

这个栈里第一个元素,如果位置  在那个长度为m 的区间内 就是合法的!

然后输出记录的结果即可!

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
char s[maxn];
int len,m;
set<int>ans;
stack<int>sk;
bool ok(int id){
    ans.clear();
    while(!sk.empty())sk.pop();
    int sum = 0;
    for (int i = 0; i < len; ++i){
        if (s[i] < id+'a')ans.insert(i),sum = 0;
        else if (s[i] == id+'a')sk.push(i),++sum;
        else ++sum;
        if (sum == m){
            if (sk.empty())return 0;
            int pos = sk.top();
            if (pos >= i-m+1 && pos <= i)ans.insert(pos);
            else return 0;
            sum = i-pos;
//            if (i == 1)printf("sum = %d\n",sum);
        }
    }
    return 1;
}
int main(){
    scanf("%d",&m);
    scanf("%s",s);
    len = strlen(s);
    for (int i = 0; i < 26; ++i){
        if (ok(i)) break;
    }
    string t = "";
    while(!ans.empty()){
        t += s[*(ans.begin())];
        ans.erase(ans.begin());
    }
    sort(t.begin(),t.end());
    printf("%s\n",t.c_str());


    return 0;
}

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".




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值