Alyona and Strings
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121333#problem/D
Description
After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar.
Alyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants.
Formally, Alyona wants to find a sequence of k non-empty strings p1, p2, p3, ..., pk satisfying following conditions:
s can be represented as concatenation a1p1a2p2... akpkak + 1, where a1, a2, ..., ak + 1 is a sequence of arbitrary strings (some of them may be possibly empty);
t can be represented as concatenation b1p1b2p2... bkpkbk + 1, where b1, b2, ..., bk + 1 is a sequence of arbitrary strings (some of them may be possibly empty);
sum of the lengths of strings in sequence is maximum possible.
Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence.
A substring of a string is a subsequence of consecutive characters of the string.
Input
In the first line of the input three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10) are given — the length of the string s, the length of the string t and Alyona's favourite number respectively.
The second line of the input contains string s, consisting of lowercase English letters.
The third line of the input contains string t, consisting of lowercase English letters.
Output
In the only line print the only non-negative integer — the sum of the lengths of the strings in a desired sequence.
It is guaranteed, that at least one desired sequence exists.
Sample Input
Input
3 2 2
abc
ab
Output
2
Input
9 12 4
bbaaababb
abbbabbaaaba
Output
7
题意:
在两个字符串中找k个不相交子串,且它们在两个原串中出现的顺序一致(字串间可以任意间隔);
题解:
四维DP[i][j][k][0/1]:
两串分别比较到第i、j个字符时,当前子串个数为k,0/1分别表示当前串能否继续向后延伸;
状态转移方程:
if(a[i]==b[j]) {
dp[i][j][k][1] = max(dp[i-1][j-1][k][1]+1, dp[i-1][j-1][k-1][0]+1);
}
dp[i][j][k][0] = max(max(dp[i-1][j][k][0], dp[i][j-1][k][0]), dp[i][j][k][1]);
//第二个等式不需要else时才执行;
WA:满足延伸条件时也可以选择不延伸:即上述方程中:
dp[i][j][k][0] = max(..., dp[i][j][k][1]);
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int z;
char a[maxn], b[maxn];
int dp[maxn][maxn][20][2];
int len1, len2;
void longest_common_substring() {
memset(dp, 0, sizeof(dp));
for(int i=1; i<=len1; i++) {
for(int j=1; j<=len2; j++) {
for(int k=1; k<=z; k++){
if(a[i]==b[j]) {
dp[i][j][k][1] = max(dp[i-1][j-1][k][1]+1, dp[i-1][j-1][k-1][0]+1);
}
dp[i][j][k][0] = max(max(dp[i-1][j][k][0], dp[i][j-1][k][0]),dp[i][j][k][1]);
}
}
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d %d %d",&len1,&len2,&z) != EOF)
{
scanf("%s %s", a+1,b+1);
a[0] = b[0] = 0;
longest_common_substring();
int ans = max(dp[len1][len2][z][0], dp[len1][len2][z][1]);
if(ans<z) ans = 0;
printf("%d
", ans);
}
return 0;
}