String(有注释)-hduoj 6586-2019 Multi-University Training Contest 1-2019暑期杭电多校训练营第一场-1009(字符串)
source:hduoj 6586
Problem Description
Tom has a string containing only lowercase letters. He wants to choose a subsequence of the string whose length is k and lexicographical order is the smallest. It’s simple and he solved it with ease.
But Jerry, who likes to play with Tom, tells him that if he is able to find a lexicographically smallest subsequence satisfying following
26
26
26 constraints, he will not cause Tom trouble any more.
The constraints are: the number of occurrences of the ith letter from
a
a
a to
z
z
z (indexed from
1
1
1 to
26
26
26) must in [
L
i
L_i
Li,
R
i
R_i
Ri].
Tom gets dizzy, so he asks you for help.
Input
The input contains multiple test cases. Process until the end of file.
Each test case starts with a single line containing a string
S
(
∣
S
∣
≤
105
)
S(|S|≤105)
S(∣S∣≤105)and an integer
k
(
1
≤
k
≤
∣
S
∣
)
k(1≤k≤|S|)
k(1≤k≤∣S∣).
Then
26
26
26 lines follow, each line two numbers
L
i
,
R
i
(
0
≤
L
i
≤
R
i
≤
∣
S
∣
)
L_i,R_i(0≤L_i≤R_i≤|S|)
Li,Ri(0≤Li≤Ri≤∣S∣).
It’s guaranteed that S consists of only lowercase letters, and
∑
∣
S
∣
≤
3
×
105
∑|S|≤3×105
∑∣S∣≤3×105.
题意
汤姆有一个只包含小写字母的字符串。他想选择一个长度为k且词典编纂顺序最小的字符串的子序列。这很简单,他很容易就解决了。
但喜欢和汤姆玩的杰瑞告诉他,如果他能找到一个符合26个约束条件的字典最小子序列,他就不会再给汤姆带来麻烦了。
约束条件是:第i个字母从a到z(索引为 1 1 1到 26 26 26)的出现次数必须在 [ l i , r i ] [l_i,r_i] [li,ri]中。
汤姆头晕了,所以他向你求助。
输入
输入包含多个测试用例。处理直到文件结束。
每个测试用例都以包含字符串 s ( s ≤ 105 ) s(s≤105) s(s≤105)和整数 k ( 1 ≤ k ≤ s ) k(1≤k≤s) k(1≤k≤s)的单行开始。
然后26行,每行两个数字 l i , r i ( 0 ≤ l i ≤ r i ≤ s ) l_i,r_i(0≤l_i≤r_i≤s) li,ri(0≤li≤ri≤s)。
保证S只包含小写字母, ∑ ∣ S ∣ ≤ 3 × 105 ∑|S|≤3×105 ∑∣S∣≤3×105。
Output
Output the answer string.
If it doesn’t exist, output
−
1
−1
−1.
Sample Input
aaabbb 3
0 3
2 3
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Sample Output
abb
题解
一位位地构造答案字符串,每次贪心地加能加入的最小的字符 (判断能否加入只要判断加入之后原字符串剩下的后缀中的每种字符的数目能否足够满足条件)。
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <list>
#include <string>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
char a[maxn];
char ans[maxn];
int k;
int l[26], r[26];
vector<int> g[26];//每个字符的下标集合
int cnt[26][maxn];//每个字符在第i位之后还有多少个
int used[26];
int main() {
while (cin >> a >> k) {
memset(cnt, 0, sizeof(cnt));
memset(used, 0, sizeof(used));
_for(i, 26) g[i].clear();
_for(i, 26) cin >> l[i] >> r[i];
int n = strlen(a);
for(int i = n - 1; i >= 0; i--) {
_for(j, 26) {
cnt[j][i] += cnt[j][i + 1] + (a[i] == 'a' + j);
}
}
_for(i, n) {
g[a[i] - 'a'].push_back(i);
}
int last = -1; //上一个选取的字符的下标
bool f1;
_for(i, k) {
f1 = 0;//能否找到可以填入的字符
_for(j, 26) {
if (r[j] == used[j]) continue;
//检查序列后面还有没有$ 'a' + j $字符
int it = 0;
for (it = 0; it < g[j].size() && g[j][it] <= last; it++);
if (it == g[j].size()) continue;
int pos = g[j][it];//这次将要选取的字符在原序列中的下标
//假设 $ 'a' + j $字符可以被选取,以下验证
int f = 1;
used[j]++;
//检验选取当前字符后新的后缀余量是否满足剩余要求
int sum = 0;
_for(t, 26) {
if (cnt[t][pos] + used[t] < l[t]) f = 0;
sum += max(l[t] - used[t], 0);
}
//剩余总字符小于需要的总字符
if (sum > k - i - 1) f = 0;
if (!f) used[j]--;
else {
ans[i] = 'a' + j;
f1 = 1;
last = pos;
break;
}
}
if (!f1) {
cout << -1 << "\n";
break;
}
}
if (f1) {
ans[k] = '\0';
cout << ans << "\n";
}
}
return 0;
}