描述:
题意:
给你一个字符串,让你从中选出一串字符,使得这串字符的sort后字典序最小,选字符的要求是如果上一个选的是i 那么[i+1,i+m]中至少选一个,[1,m]中必须选一个
思路:
贪心,先考虑只由'a'-c的字符组成该字符串,首先有‘a’的话肯定是拿'a'越多越好,拿'b'也是越多越好(aaabbXX肯定比aaabXXX字典序小),也就c字符需要特判一下,因为c字符不是越多越好,而是越少越好,只有在满足前面的越多越好的前提下,不得不拿c才拿。
有了上面的策略我们具体做的时候就先满足区间长度不超过m的条件扫一遍字符串,再按字典序小的加入生成的字符串中策略扫一遍字符串,然后就好了。
比赛的时候贪心策略少考虑了点_(:зゝ∠)_ ,贪心没想象中这么简单!!!
代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
#define mod 1000000007
using namespace std;
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)
const int N=1e5+10;
char s[N],a[N],mx='a';
int vis[N],top=0,m;
int main(){
scanf("%d",&m);
scanf("%s",s+1);
int n=strlen(s+1);
int now=1;
while(now+m-1<=n){
int res=now;
rep(i, now+1, now+m-1){
if(s[i]<=s[res])res=i;
}a[++top]=s[res];
vis[res]=1;
mx=max(mx, a[top]);
now=res+1;
}
rep(i, 1, n)if(!vis[i] && s[i]<mx)a[++top]=s[i];//贪心把字典序小的加入生成的字符串中
sort(a+1, a+top+1);
printf("%s\n", a+1);
return 0;
}