2020/5/1
1.字符串dp
Problem - 1183H - Codeforces
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string “abaca” the following strings are subsequences: “abaca”, “aba”, “aaa”, “a” and “” (empty string). But the following strings are not subsequences: “aabaca”, “cb” and “bcaa”.
You are given a string ss consisting of nn lowercase Latin letters.
In one move you can take any subsequence tt of the given string and add it to the set S S S The set S S S can’t contain duplicates. This move costs n − ∣ t ∣ n−|t| n−∣t∣, where ∣ t ∣ |t| ∣t∣ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S S S of size k k k or report that it is impossible to do so.
Input
The first line of the input contains two integers n n n and k ( 1 ≤ n ≤ 100 , 1 ≤ k ≤ 1 0 12 ) k (1≤n≤100,1≤k≤10^{12}) k(1≤n≤100,1≤k≤1012) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string ss consisting of nn lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S S S of size k k k, print -1. Otherwise, print the minimum possible total cost to do it.
题意大概就是给出一个长度为n的字符串,求你找出k个子序列,每个子序列
t
t
t对答案贡献
n
−
∣
t
∣
n−|t|
n−∣t∣,求最小贡献,找不到就是-1。
很明显就是要找出不同长度本质不同的子序列的数量,如果只是求本质不同的字符串的数量那就是
d
p
[
i
]
=
d
p
[
i
−
1
]
∗
2
−
d
p
[
l
a
s
t
[
s
[
i
]
]
dp[i]=dp[i-1]*2-dp[last[s[i]]
dp[i]=dp[i−1]∗2−dp[last[s[i]]。
但是这题还需要各个长度的,这个n也比较小,于是再开一维
d
p
[
i
]
[
j
]
dp[i][j]
dp[i][j]表示前
i
i
i个字符中删除了
j
j
j个字符得到的子序列数量。
d
p
[
i
]
[
j
]
=
d
p
[
i
−
1
]
[
j
]
dp[i][j]=dp[i-1][j]
dp[i][j]=dp[i−1][j](删除当前这个字符)+
d
p
[
i
−
1
]
[
j
−
1
]
dp[i-1][j-1]
dp[i−1][j−1](加上这个字符)得到的字符串。
一样考虑重复问题,还需要减去
i
−
1
−
j
=
=
l
a
s
t
[
s
[
i
]
]
−
1
−
x
中
的
x
i-1-j==last[s[i]]-1-x中的x
i−1−j==last[s[i]]−1−x中的x,故再
−
d
p
[
l
a
s
t
[
s
[
i
]
]
−
1
]
[
l
a
s
t
[
s
[
i
]
−
(
i
−
j
)
]
-dp[last[s[i]]-1][last[s[i]-(i-j)]
−dp[last[s[i]]−1][last[s[i]−(i−j)]
ll n, k;
char s[maxn];
cin >> n >> k >> s + 1;
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1));
vector<int> last(300, -1);
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
dp[i][0] = 1;
for (int j = 1; j <= i; ++j) {
dp[i][j] += dp[i - 1][j - 1] + dp[i - 1][j];
if (last[s[i]] >= (i - j)) {
dp[i][j] -= dp[last[s[i]] - 1][last[s[i]] - (i - j)];
}
}
last[s[i]] = i;
}
ll ans = 0;
for (int i = 0; i <= n; ++i) {
ans += min(k, dp[n][i]) * i;
k -= min(k, dp[n][i]);
if (k == 0) {
cout << ans << endl;
return 0;
}
}
cout << -1 << endl;
2.思维 DP
Problem - 1197D - Codeforces
You are given an array
a
1
,
a
2
,
…
,
a
n
a_1,a_2,…,a_n
a1,a2,…,an and two integers m and k.
You can choose some subarray a l , a l + 1 , … , a r − 1 , a r . a_l,a_{l+1},…,a_{r−1},a_r. al,al+1,…,ar−1,ar.
The cost of subarray a l , a l + 1 , … , a r − 1 , a r . a_l,a_{l+1},…,a_{r−1},a_r. al,al+1,…,ar−1,ar. is equal to ∑ i = l r a i − k ⌈ r − l + 1 m ⌉ \sum_{i=l}^ra_i-k⌈\frac{r−l+1}{m}⌉ ∑i=lrai−k⌈mr−l+1⌉, where ⌈x⌉ is the least integer greater than or equal to x.
The cost of empty subarray is equal to zero.
For example, if m=3, k=10 and a=[2,−4,15,−3,4,8,3], then the cost of some subarrays are:
a
3
…
a
3
:
15
−
k
⌈
1
3
⌉
=
15
−
10
=
5
;
a_3…a_3:15−k⌈\frac{1}{3}⌉=15−10=5;
a3…a3:15−k⌈31⌉=15−10=5;
a
3
…
a
4
:
(
15
−
3
)
−
k
⌈
2
3
⌉
=
12
−
10
=
2
;
a_3…a_4:(15−3)−k⌈\frac{2}{3}⌉=12−10=2;
a3…a4:(15−3)−k⌈32⌉=12−10=2;
a
3
…
a
5
:
(
15
−
3
+
4
)
−
k
⌈
3
3
⌉
=
16
−
10
=
6
;
a_3…a_5:(15−3+4)−k⌈\frac{3}{3}⌉=16−10=6;
a3…a5:(15−3+4)−k⌈33⌉=16−10=6;
a
3
…
a
6
:
(
15
−
3
+
4
+
8
)
−
k
⌈
4
3
⌉
=
24
−
20
=
4
;
a_3…a_6:(15−3+4+8)−k⌈\frac{4}{3}⌉=24−20=4;
a3…a6:(15−3+4+8)−k⌈34⌉=24−20=4;
a
3
…
a
7
:
(
15
−
3
+
4
+
8
+
3
)
−
k
⌈
5
3
⌉
=
27
−
20
=
7.
a_3…a_7:(15−3+4+8+3)−k⌈\frac{5}{3}⌉=27−20=7.
a3…a7:(15−3+4+8+3)−k⌈35⌉=27−20=7.
Your task is to find the maximum cost of some subarray (possibly empty) of array a.
Input
The first line contains three integers n, m, and k
(
1
≤
n
≤
3
⋅
1
0
5
,
1
≤
m
≤
10
,
1
≤
k
≤
1
0
9
)
.
(1≤n≤3⋅10^5,1≤m≤10,1≤k≤10^9).
(1≤n≤3⋅105,1≤m≤10,1≤k≤109).
The second line contains n integers a 1 , a 2 , … , a n ( − 1 0 9 ≤ a i ≤ 1 0 9 ) . a_1,a_2,…,a_n (−10^9≤a_i≤10^9). a1,a2,…,an(−109≤ai≤109).
Output
Print the maximum cost of some subarray of array a.
题意就是选择子串
[
l
,
r
]
[l,r]
[l,r]使得
∑
i
=
l
r
a
i
−
k
⌈
r
−
l
+
1
m
⌉
\sum_{i=l}^ra_i-k⌈\frac{r−l+1}{m}⌉
∑i=lrai−k⌈mr−l+1⌉最大
有点像最大子段和,但是不是。不过也是dp思想。
d
p
[
i
]
[
j
]
dp[i][j]
dp[i][j]表示右端点为i,长度
(
l
e
n
−
1
)
%
m
=
j
(len-1)\%m=j
(len−1)%m=j的子串的贡献。
因为每过边界m,就减一个k,此时就可以抉择,是否重新开始。
- (j==0,即长度%m==1) d p [ i ] [ j ] = m a x ( d p [ i − 1 ] [ m − 1 ] , 0 ) + a [ i ] − k dp[i][j]=max(dp[i-1][m-1],0)+a[i]-k dp[i][j]=max(dp[i−1][m−1],0)+a[i]−k
- (j!=0)
d
p
[
i
]
[
j
]
=
d
p
[
i
−
1
]
[
j
−
1
]
+
a
[
i
]
dp[i][j]=dp[i-1][j-1]+a[i]
dp[i][j]=dp[i−1][j−1]+a[i]未达边界,不必-k
d p 起 点 为 d p [ 0 ] [ 0 ] = a [ 0 ] − k dp起点为dp[0][0] = a[0] - k dp起点为dp[0][0]=a[0]−k,即右端点在0位置,长度为1时的ans。
两个小细节
- ans初始化是 m a x ( a [ 0 ] − k , 0 l l ) ; max(a[0] - k, 0ll); max(a[0]−k,0ll);
- 以及二重循环 j < m i n ( i + 1 , m ) j < min(i + 1, m) j<min(i+1,m)因为太少不够选择是无法被转移的。
ll n, m, k;
cin >> n >> m >> k;
vector<ll> a(n);
for (auto &it : a) cin >> it;
vector<vector<ll>> dp(n, vector<ll>(m, 0));
ll ans = max(a[0] - k, 0ll);
dp[0][0] = a[0] - k;
for (ll i = 1; i < n; ++i) {
for (ll j = 0; j < min(i + 1, m); ++j) {
if (j == 0)
dp[i][j] = max(dp[i - 1][m - 1] + a[i], a[i]) - k;
else
dp[i][j] = dp[i - 1][j - 1] + a[i];
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;