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 ≤ 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=3;

现在给了你一段序列

从1开始。

1-3里面必然要选一个

那么选什么呢?

肯定是选字典序最小的那个。

不然的话最后选出来的一定不是最优的

比如cab

你只能选a.

然后再从xiabiao[a]+1开始再选3个

cabbb

现在下标变成3

你需要从bbb中选择一个。当然还是选择最小的那个,这个时候你没得选了

只能选b.(记录字典序最大的变成了b)

但是为了节省时间。你需要尽量往后选。

比如cabbbzab

因为如果你选了第一个b

接下来就要从b,b,z这三个里面选一个最小的。

那还是b。。所以没有意义,直接从最后一个选开始选就好。



b z z z b

这样的数据如果不从最后一个b开始选。你会面临3个z的抉择。这下可就会错解了。

每次选定一个字母后,就尝试更新最大字典序的字母;

最后获得了最大字典序的字母key;

从’a’..’key’-1;按顺序添加到答案字符串的末尾;

对于key要单独处理。只有在m个m个地选的过程中不得不选key这个字符的时候才要选,即加到答案字符串末尾;

因为

假如key为b

aaabb小于aaabbb

即等于key的不能全选。那样可能会出错;

而之所以小于key的要全选

是因为

aabb小于abb

即从第一个不同的字母开始比较的。

和Pascal有点不一样。pascal是先比较长度。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string> const int MAXN = 2e5; using namespace std;
vector <int> a[30];
string s;
bool vis[MAXN] = { 0 };
int m; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(m);
cin >> s;
int len = s.size();
for (int i = 0; i <= len - 1; i++)
a[s[i] - 'a'].push_back(i);
int i,mx = 0;
for (i = 0; i <= len - m; i++)
{
for (int j = 0; j <= 25; j++)//从小到大可以肯定找的是字典序最小的。
{
int k = upper_bound(a[j].begin(), a[j].end(), i + m - 1) - a[j].begin();//upper_bound则尽量靠后
k--;
int len = a[j].size();
if (k >= 0 && k <= len-1 && a[j][k] >= i && a[j][k] <= i + m - 1)
{
mx = max(mx, j);//找到之后就尝试更新最大字典序
i = a[j][k];//等于它的位置就好第一层for会+1
vis[a[j][k]] = true;//记录这个位置必须选
break;//记录这个是为了方便控制输出mx字符
}
}
}
string ans = "";
for (int i = 0; i < mx; i++)
{
len = a[i].size();
for (int j = 0; j <= len - 1; j++)
ans += (i + 'a');
}
len = a[mx].size();
for (int j = 0; j <= len - 1; j++)
if (vis[a[mx][j]])
ans += (mx + 'a');
cout << ans << endl;
return 0;
}

【29.41%】【codeforces 724D】Dense Subsequence的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【35.29%】【codeforces 557C】Arthur and Table

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【41.43%】【codeforces 560C】Gerald's Hexagon

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 29B】Traffic Lights

    [题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  9. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

随机推荐

  1. HTTP网络协议(五)

    主动攻击:是指攻击通过直接访问Web应用,把攻击代码传入的攻击模式,该模式是直接针对服务器上的资源进行攻击,因此攻击者需要能够访问到那些资源,例如:SQL注入攻击和OS命令注入攻击.  被动攻击:是指 ...

  2. Android 使用XML隐藏ActionBar中遇错的解决的方法

    今天我在使用Menifest.xml让程序隐藏标题栏是一直出错.主要内容是: You need to use a theme.AppCompat theme(descendant) with this ...

  3. BZOJ 3038 上帝造题的七分钟2 树状数组+并查集

    题目大意:一个序列,有两种操作.1.将一段数中的每个数开根号.2.查询一段数的和. 思路:和3211是一个题,有兴趣的能够看看我的那篇博客. CODE: #include <cmath> ...

  4. Sass(SCSS)中文手册——入门

    简书原文 https://www.jianshu.com/p/e82c27aa05c7 前言 该中文手册是我在Sass中文文档的基础上编辑的,或者也可以理解为就是Sass中文文档的翻版.之所以有这篇文 ...

  5. vue中的select框的值动态绑定

    <--这两种写法效果一样--> 1: <select v-model="wxStatus"> <option label="已添加" ...

  6. UVA 11889 - Benefit 可直接枚举

    看题传送门 题目大意: 输入两个整数A和C,求最小的整数B,使得lcm(A,B)=C.如果无解,输出NO SOLUTION 思路: A*B=C*gcd(A,B) 所以 B / gcd(A,B) = C ...

  7. [Angular] Learn How To Use ng-template Inputs

    For example, we have a modal component, it can config that using ng-template as a configurable templ ...

  8. Spark Streaming教程

      废话不说,先来个示例,有个感性认识再介绍. 这个示例来自spark自带的example,基本步骤如下: (1)使用以下命令输入流消息: $ nc -lk 9999 (2)在一个新的终端中运行Net ...

  9. squeeze() 函数——MATLAB

    B=squeeze(A) 移除张量A的单一维,即返回和矩阵A元素相同,但所有单一维都移除的矩阵B,单一维是满足size(A,dim)=1的维. squeeze命令对二维数组是不起作用的; 如果A是一行 ...

  10. UVA 1156 - Pixel Shuffle(模拟+置换)

    UVA 1156 - Pixel Shuffle 题目链接 题意:依据题目中的变换方式,给定一串变换方式,问须要运行几次才干回复原图像 思路:这题恶心的一比,先模拟求出一次变换后的相应的矩阵,然后对该 ...