题解 CF1267L 【Lexicography】
贪心,尽量把大的留给第k个,假设当前枚举到第i个,则只需要将字符从小到大排列在前i-1个都与第k个相同且位置小于k的,可以证明这样是最优的。输出时如果当前位置有字符则输出该字符,否则输出未使用字符最小的一个。
代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define in inline
#define re register
using namespace std;
in int read() {
re int t=0;
re char v=getchar();
while(v<'0'||v>'9'){
v=getchar();
}
while(v>='0'&&v<='9'){
t=(t<<3)+(t<<1)+(v^48);
v=getchar(); }
return t;
}
char ans[1002][1002],s[1000002];
int n,l,k,cnt[2006],fst[2006],tot;
inline void work(re int x,re int a){
if(x>l)return;
memset(fst,0,sizeof(fst));
for(re int i=a;i<=k;++i){
ans[i][x]=s[tot++];
if(!fst[s[tot-1]])fst[s[tot-1]]=i;
}
work(x+1,fst[ans[k][x]]);
}
int main(){
scanf("%d %d %d",&n,&l,&k); scanf("%s",s);
sort(s,s+n*l);
work(1,1);
for(re int i=1;i<=n;++i){
for(re int j=1;j<=l;++j){
if(ans[i][j])putchar(ans[i][j]);
else putchar(s[tot++]);
}
putchar('\n');
}
}