codeforces 358D. Alyona and Strings (dp)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjw6463/article/details/51943173
D. Alyona and Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
Input
3 2 2
abc
ab
Output
2
Input
9 12 4
bbaaababb
abbbabbaaaba
Output
7
Note

The following image describes the answer for the second sample case:

扫描二维码关注公众号,回复: 4361161 查看本文章




题意:给你两个字符串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]);

	}
}




猜你喜欢

转载自blog.csdn.net/zjw6463/article/details/51943173