• Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence(贪心)


    D. Dense Subsequence

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

    题意是给出整数m和一个字符串,要求从所给的字符串中选出一个字符任意顺序组成一个字符串,要求新的字符串在所有符合条件的字符串中字典序最小,条件是如果选择了第i个字符那么在下标在[i+1, i + 1+m]范围内的字符至少被选一个。

    做法是枚举字母,显然如果以该字母作为字符串中的最大字母,那么比这个字母小的字母都需要被选中才会使得字典序最小,而这个字母不一定都要被选中所以我们在一定范围内只找最右边的这个字母,而比这个字母小的所有字母都要被选中。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5 + 10;
    char s[maxn];
    int vis[maxn];
    int n, m;
    
    int work(int c)
    {
        int pre = -1, now = -1, res = 0;
        for(int i = 0; i < n; i++) {
            if(s[i] - 'a' < c) pre = i, now = i;
            if(s[i] - 'a' == c) now = i;
            if(i - pre == m) {
                //printf("check %d %d %d
    ", i, c, pre);
                if(now > pre) {
                    res++;
                    pre = now;
                }
                else return -1;
            }
        }
        return res;
    }
    
    int main() 
    {
        scanf("%d %s", &m, s);
        memset(vis, 0, sizeof(vis));
        n = strlen(s);
        for(int i = 0; i < n; i++) {
            vis[s[i] - 'a']++;
        }
        for(int i = 0; i < 26; i++) {
            int flag = work(i);
            if(flag != -1) {
                for(int j = 0; j < i; j++) {
                    for(int k = 0; k < vis[j]; k++) {
                        printf("%c", j + 'a');
                    }
                }
                for(int j = 0; j < flag; j++) {
                    printf("%c", i + 'a');
                }puts("");
                return 0;
            }
        }
    }
  • 相关阅读:
    Python shutil模块(目录和文件操作)
    CentOS8的web终端-cockpit
    OpenStack与ZStack深度对比:架构、部署、计算、运维监控等
    Zstack的安装部署
    Git介绍与简易搭建
    Docker 底层技术与端口映射
    Docker 网络
    Dockerfile编写
    Docker监控
    Docker 中卷组管理
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5941814.html
Copyright © 2020-2023  润新知