After returned from forest, Alyona started reading a book. She noticed stringss and t, lengths of which aren and m respectively. As usual, reading bored Alyona and she decided to pay her attention to stringss 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 choosek disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of stringt and in the same order as they do in strings. 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 stringsp1, p2, p3, ..., pk satisfying following conditions:
- s can be represented as concatenation a1p1a2p2...akpkak + 1, wherea1, 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, whereb1, 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.
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 strings, 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.
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.
3 2 2 abc ab
2
9 12 4 bbaaababb abbbabbaaaba
7
The following image describes the answer for the second sample case:
题意:给你两个长度分别为N和M的字符串,现在让你从两个字符串中挑出k个不重叠的子串,且这些子串在两个字符串中出现的次序一样,问你这k个子串的最长长度为多少。
分析:简单DP,F[i][j]表示第一个字符串的i位置和第二个字符串的j位置开始的最长前缀的长度,F[i][j] = F[i-1][j-1]+1 (a[i-1] == b[j-1]),又设dp[i][j][k] 表示现在匹配到第一个字符串中的i和第二个字符串中的j且之前已经匹配了k-1个子串的最长长度,那么dp[i][j][k] = max(dp[i-1][j][k],dp[i][j-1][k]),若F[i][j]存在则还有dp[i][j][k] = max(dp[i][j][k],dp[i-F[i][j]][j-F[i][j]][k-1] + F[i][j]).
#include <cstdio>
#include <queue>
#include <vector>
#include <cstdio>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
int n,m,k,ans,f[1005][1005],dp[1005][1005][11];
char a[1005],b[1005];
using namespace std;
int main()
{
cin.sync_with_stdio(false);
cin>>n>>m>>k;
cin>>a;
cin>>b;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(a[i-1] == b[j-1]) f[i][j] = 1 + f[i-1][j-1];
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
for(int l = 1;l <= k;l++)
{
dp[i][j][l] = max(dp[i-1][j][l],dp[i][j-1][l]);
if(f[i][j]) dp[i][j][l] = max(dp[i][j][l],dp[i-f[i][j]][j-f[i][j]][l-1]+f[i][j]);
ans = max(ans,dp[i][j][l]);
}
cout<<ans<<endl;
}