codeforces 358D. Alyona and Strings (dp)
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
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
- 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.
InputIn 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.
OutputIn 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.
Examples Input3 2 2 abc abOutput
2Input
9 12 4 bbaaababb abbbabbaaabaOutput
7Note
The following image describes the answer for the second sample case:
題意:給你兩個字串s 和 t , 然後在s中找k個不重疊的子串, 並且能夠在t中按順序 能找出這k個子串, 求這k個子串的最大長度和
dp[i][j][p][q] ,i是在s中的位置, j是在t中的位置,i,j跟求最大公共子串一樣, p是當前子串的個數, q為1說明還能繼續匹配子串個數不用加, 0說明匹配結束要加個子串繼續匹配
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define esp 1e-8
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int inf = 2147483647;
const long long mod = 1000000007;
typedef long long ll;
//freopen("in.txt","r",stdin); //輸入重定向,輸入資料將從in.txt檔案中讀取
//freopen("out.txt","w",stdout); //輸出重定向,輸出資料將儲存在out.txt檔案中
char s[1005], t[1005];
int dp[1005][1005][15][2];
int main()
{
int n, m, k, i, j;
while (~scanf("%d%d%d", &n, &m, &k))
{
scanf("%s%s", s + 1, t + 1);
memset(dp, 0, sizeof(dp));
// cout << s + 1 << endl;
// cout << t + 1 << endl;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m; ++j)
{
if (s[i] == t[j])
{
for (int p = 1; p <= k; ++p)
dp[i][j][p][1] = max(dp[i - 1][j - 1][p - 1][0], dp[i - 1][j - 1][p][1]) + 1;
}
for (int p = 1; p <= k; ++p)
{
dp[i][j][p][0] = max(max(dp[i - 1][j][p][0], dp[i][j - 1][p][0]), max(dp[i - 1][j - 1][p][0], dp[i][j][p][1]));
}
}
}
printf("%d\n", dp[n][m][k][0]);
}
}