题目链接:https://cn.vjudge.net/problem/HDU-6586
题意:给出一个序列,找到长度为k的字典序最小的子序列,并且给出每个字符的数量的限制
题解:这个和计蒜客复赛的题很像,链接:https://blog.csdn.net/mmk27_word/article/details/92417741,但是这个给出了每个字符数量的限制,那么我们就预处理记录下每个位置后面每个字符的数量,和每个字符的位置,这里我们就通过位置关系,枚举字符来判断要这个字符是否符合条件,l和r记录该字符还需要多少个,记录两个值,minn += l[z](sum[v[j][nowl[j]]][z] >=l[z]) maxx += min(sum[v[j][nowl[j]]][z], r[z]);表示后面字符能形成的数量,(k - i ) >= minn && (k - i ) <= maxx既符合条件。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int sum[N][27];
int l[27], r[27];
char s[N];
int k;
int v[27][N];
int lenv[27], nowl[27];
char ans[N];
int main() {
int minn, maxx, len, pl, pr;
int flag, flagcnt;
int pos;
while(~scanf("%s %d", s + 1, &k)) {
for(int i = 0; i <= 25; i++) scanf("%d %d", &l[i], &r[i]);
len = strlen(s + 1);
for(int i = 0; i <= 25; i++) sum[len][i] = 0, lenv[i] = 0, nowl[i] = 0;
v[s[len] - 'a'][lenv[s[len] - 'a'] ++] = len;
for(int i = len - 1; i >= 1; i--) {
for(int j = 0; j <= 25; j++)
sum[i][j] = sum[i + 1][j];
sum[i][s[i + 1] - 'a']++;
v[s[i] - 'a'][lenv[s[i] - 'a']++] = i;
}
for(int i = 0; i <= 25; i++)
sort(v[i], v[i] + lenv[i]);
pos = 0;
for(int i = 1; i <= k; i++ ) {
flag = 0;
for(int j = 0; j <= 25; j++) {
while(nowl[j] < lenv[j] && v[j][nowl[j]] <= pos) nowl[j]++;
if(nowl[j] == lenv[j] || r[j] <= 0) continue;
flagcnt = 1;
minn = 0;
maxx = 0;
pl = l[j], pr = r[j];
l[j] = max(l[j] - 1, 0);
r[j] = max(r[j] - 1, 0);
for(int z = 0; z <= 25; z++) {
if(sum[v[j][nowl[j]]][z] >= l[z] ) {
minn += l[z];
maxx += min(sum[v[j][nowl[j]]][z], r[z]);
} else {
flagcnt = 0;
break;
}
}
if(flagcnt && (k - i ) >= minn && (k - i ) <= maxx) {
flag = 1;
ans[i] = j + 'a';
pos = v[j][nowl[j]];
break;
} else {
l[j] = pl; r[j] = pr;
}
}
if(!flag) break;
}
ans[k + 1] = '\0';
if(flag) printf("%s\n", ans + 1);
else printf("-1\n");
}
return 0;
}