您的位置:首页 > 产品设计 > UI/UE

CodeForce 724D. Dense Subsequence bY Assassin

2016-10-09 21:42 253 查看
D. Dense Subsequence
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


这题超级简单你敢信?就是个贪心。。。每次从头开始,然后每次从m个中选出最小的,下一次从上次的最小开始,直到结束,每次记录一下次数,吧用到的最大的字母挑出来,小于他的原字符串有多少输出多少,最大的那个用到多少输出多少。。。真蠢这题错了10次?还不会看了别人的代码。。。

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
char a[100010];
int c[26];
int use[26];
int m;
int main()
{
input;
int i,j;
string s;
while(scanf("%d",&m)!=EOF)
{
memset(c,0,sizeof(c));
memset(use,0,sizeof(use));
cin>>s;
for(i=0;i<s.length();i++)
{
a[i+1]=s[i];
c[s[i]-'a']++;
}
int len=s.size();
int pos=0,p,maxx,minn;
for(maxx=0,pos=0;pos+m<=len;)
{
minn=26;
p=pos;
for(i=1;i<=m;i++)
{
if(a[pos+i]-'a'<=minn)
{
minn=a[pos+i]-'a';
p=i+pos;
}
}
use[a[p]-'a']++;
if(maxx<a[p]-'a')
{
maxx=a[p]-'a';
}
pos=p;
}
for(i=0;i<maxx;i++)
for(j=1;j<=c[i];j++)
printf("%c",i+'a');
for(i=1;i<=use[maxx];i++)printf("%c",maxx+'a');
cout<<endl;
}
return 0;
}


膜拜zchahaha
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: