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

题意:给你一个长度至多1e5的字符串,和一个数字m(<=1e5),要求你找出一组下标,使得任意的长度为m的连续的字符串序列都包括你选的下标,输出字典序最小的一组解;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
#define SC scanf
typedef long long ll;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int N=1e5+;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;}; char s[N];
int num[];
int main()
{
int m;
while(~SC("%d",&m)){
MM(num,);
SC("%s",s+);
char c;
for(c='a';c<='z';c++){
int k=,flag=;
for(int i=;s[i]!='\0';i++){
k++;
if(s[i]<=c) {
k=;
if(s[i]==c){
num[c-'a']++;
}
}
if(k>=m) flag=;
}
if(flag) break;
}
num[c-'a']=; int k=,now;
for(int i=;s[i]!='\0';i++){
k++;
if(s[i]<c) k=;
else if(s[i]==c) now=i;
if(k==m) {
num[c-'a']++;
i=now;
k=;
}
}
for(char x='a';x<=c;x++)
{
while(num[x-'a']--) {
printf("%c",x);
}
}
printf("\n");
}
return ;
}

  分析:

1.首先可以发现从从a到z枚举出选出的字符的最大值的话,那么要保证字典序最小的话,比当前枚举的字符小的都要选取(二分思想)

2.那么我们只要从a到z枚举选取的字符的最大值qq,首先所有qq都选,判断能否满足题意;

3.接下来只要确定qq的个数就好了,从左到右枚举,一当出现k==m的情况,就选取最近的一个qq

Intel Code Challenge Final Round D. Dense Subsequence 二分思想的更多相关文章

  1. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

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

    传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...

  3. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力

    D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort

    链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing

    我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  7. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)

    题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...

  8. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)

    传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...

  9. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)

    传送门 Description You are given a table consisting of n rows and m columns. Numbers in each row form a ...

随机推荐

  1. php7和PHP5对比的新特性和性能优化

    1  抽象语法树( AST) 1)在 PHP5中,从 php 脚本到 opcodes 的执行的过程是: Lexing:词法扫描分析,将源文件转换成 token 流:    Parsing:语法分析,在 ...

  2. python 基础(十七)--hashlib加密模块

    hashlib加密模块 两种方式使用 字符串是中文时需要先编码成utf-8 常用加密算法:md5,sha1(已被破解)等... >>> a= hashlib.md5() >&g ...

  3. command not found 的解决&&解释

    ln -s /opt/platform/calico/calicoctl /usr/local/sbin/calicoctl ln -s /opt/platform/nginx/sbin/nginxl ...

  4. DateTime.TryParse 日期时间字符串验证

    DateTime applicationDatetime = new DateTime(); bool applicationDate = DateTime.TryParse("2019-0 ...

  5. mysql replace substring 字符串截取处理

    SELECT a1,a2,replace(a2, "豫ICP备16006180号-", "") a22,a3,a4,a5 FROM `aaab` order b ...

  6. 配置UOJ数据的正确姿势

    最近瞎搞用开源UOJ搭了个OJ,在题目配置方面搞了挺久,一开始看vfleaking的文档还折腾了SVN,特意写下这篇文章为后来人少走弯路 Step 1 拥有管理权限并设置好题面,支持\(LaTex\) ...

  7. luogu1731生日蛋糕题解--恶心剪枝

    题目链接 https://www.luogu.org/problemnew/show/P1731 分析 这题真[哔]恶心,加了一堆奇奇怪怪的优化 首先明确一点,半径和高都必须是正整数,意味着它们最小为 ...

  8. css的一些基础方法

    1.css样式表分别有: 内联样式表 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  9. Xadmin相关实现

    一,保存搜索条件(Save search conditions) kept conditions(保存条件)的应用场景比较常见,在我们查询时,可以实现多条件的筛选查询,比如:在淘宝上,选择了其中的一个 ...

  10. Android基础相关面试问题-activity面试问题(生命周期,任务栈,启动模式,跳转协议,启动流程)

    关于Android的一些面试题在15年就已经开了这个专栏了,但是一直木有坚持收集,而每次面对想要跳槽时大脑一片空白,也有些恐惧,因为毕境面试都是纯技术的沟通,要想让公司对你的技术能有所认可会全方位的进 ...