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
  1. 3
    cbabc
output
  1. a
input
  1. 2
    abcab
output
  1. aab
input
  1. 3
    bcabcbaccba
output
  1. 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的连续的字符串序列都包括你选的下标,输出字典序最小的一组解;

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <vector>
  7. #include <queue>
  8. #include <map>
  9. #include <algorithm>
  10. #include <set>
  11. using namespace std;
  12. #define MM(a,b) memset(a,b,sizeof(a))
  13. #define SC scanf
  14. typedef long long ll;
  15. typedef unsigned long long ULL;
  16. const int mod = ;
  17. const double eps = 1e-;
  18. const int inf = 0x3f3f3f3f;
  19. const int N=1e5+;
  20. int max(int a,int b) {return a>b?a:b;};
  21. int min(int a,int b) {return a<b?a:b;};
  22.  
  23. char s[N];
  24. int num[];
  25. int main()
  26. {
  27. int m;
  28. while(~SC("%d",&m)){
  29. MM(num,);
  30. SC("%s",s+);
  31. char c;
  32. for(c='a';c<='z';c++){
  33. int k=,flag=;
  34. for(int i=;s[i]!='\0';i++){
  35. k++;
  36. if(s[i]<=c) {
  37. k=;
  38. if(s[i]==c){
  39. num[c-'a']++;
  40. }
  41. }
  42. if(k>=m) flag=;
  43. }
  44. if(flag) break;
  45. }
  46. num[c-'a']=; int k=,now;
  47. for(int i=;s[i]!='\0';i++){
  48. k++;
  49. if(s[i]<c) k=;
  50. else if(s[i]==c) now=i;
  51. if(k==m) {
  52. num[c-'a']++;
  53. i=now;
  54. k=;
  55. }
  56. }
  57. for(char x='a';x<=c;x++)
  58. {
  59. while(num[x-'a']--) {
  60. printf("%c",x);
  61. }
  62. }
  63. printf("\n");
  64. }
  65. return ;
  66. }

  分析:

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. JavaScript(js)笔记

    js注释 JavaScript注释与Java注释相同 // 单行注释 /* 多行注释 */ js五大基本类型:   number(数值型).string(字符串性).boolean(布尔型).unde ...

  2. ios 输入框失去焦点,位置回调方法

    微信网页开发,ios 在input,textarea 失去焦点后,页面无法回调. 以下方法可解决: $("input,textarea").on("blur", ...

  3. 【Python基础】06_Python中的函数

    1.函数的定义 def 函数名(): 函数封装的代码 …… 注:函数前后应该保留两个空行 2.函数的使用 直接使用函数名()调用函数块. def say_hello(): print("He ...

  4. 11.vim常用操作

    :set nu在命令模式下显示行号 进入输入模式 新增 (append) a :从光标所在位置後面开始新增资料,光标後的资料随新增资料向後移动. A:从光标所在列最後面的地方开始新增资料.   插入 ...

  5. Jmeter4.0---- jmeter中写入java代码_简单了解(15)

    1.说明 BeanShell:是一个小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法,并利用在JavaScript和Perl中常见的的松散类型.命令.闭包等通用脚本 ...

  6. luogu4302字符串折叠题解--区间DP

    题目链接 https://www.luogu.org/problemnew/show/P4302 分析 很明显一道区间DP题,对于区间\([l,r]\)的字符串,如果它的字串是最优折叠的,那么它的最优 ...

  7. C# list to dictionary

    示例: 新建一个类: public class Lang { public string En; public string Ch; } 实例化并转为字典: List<Lang> lang ...

  8. vs 调试时 QuickWatch 不能计算变量值

    尝试下这个方法:DEBUG- -> Options and Settings--> Symbols中查看缓存路径是否设置为当前程序的bin文件然后将Debug-->Options a ...

  9. 3.MySQL的架构介绍

    MySQL简介: 高级MySQL:mysql 内核 sql优化工程师 mysql服务器的优化 各种参数常量设定 查询语句优化 主从复制 软硬件升级 容灾备份 sql编程 完整的mysql优化需要很深的 ...

  10. JAVA 内存模型(主内存,工作内存)

    JVM将内存组织为主内存和工作内存两个部分. 主内存是所有的线程所共享的,主要包括本地方法区和堆. 每个线程都有一个工作内存不是共享的,工作内存中主要包括两个部分: 1:一个是属于该线程私有的栈; 2 ...