Codeforces 682D Alyona and Strings【dp】

来源:互联网 发布:阿里云网站备案 编辑:程序博客网 时间:2024/05/29 16:44

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 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.

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 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.

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 2abcab
Output
2
Input
9 12 4bbaaababbabbbabbaaaba
Output
7
Note

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

题目大意:

给你一个长度为n和一个长度为m的字符串,让你将第一个字符串分成k段,使其按照相对位子和第二个字符串匹配的长度最大。

问这个最大长度。


思路:


考虑dp,设定dp【i】【j】【k】【2】:

dp【i】【j】【k】【0】表示第一个字符串位子到i,第二个字符串位子到j.已经分配完了k段,并且当前最后一个字符不希望延展的最大长度。

dp【i】【j】【k】【1】表示第一个字符串位子到i,第二个字符串位子到j.已经分配完了k段,并且当前最后一个字符希望延展的最大长度。


那么有:

if(a【i】==b【j】)

dp【i】【j】【k】【1】=max(dp【i-1】【j-1】【k-1】【0】+1,dp【i-1】【j-1】【k-1】【1】);从不希望延展的结尾处从新开辟一段子序列。

dp【i】【j】【k】【1】=max(dp【i-1】【j-1】【k】【1】+1,dp【i-1】【j-1】【k-1】【1】);从希望延展的结尾处继续延展。

无论a【i】是否和b【j】相等都有:

dp【i】【j】【k】【0】=max(dp【i-1】【j-1】【k】【0】,dp【i】【j】【k】【0】);

dp【i】【j】【k】【0】=max(dp【i-1】【j】【k】【0】,dp【i】【j】【k】【0】);

dp【i】【j】【k】【0】=max(dp【i】【j-1】【k】【0】,dp【i】【j】【k】【0】);

dp【i】【j】【k】【0】=max(dp【i】【j】【k】【1】,dp【i】【j】【k】【0】);


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char a[1500];char b[1500];int dp[1005][1005][12][2];int main(){    int n,m,kk;    while(~scanf("%d%d%d",&n,&m,&kk))    {        memset(dp,0,sizeof(dp));        scanf("%s",a+1);        scanf("%s",b+1);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                for(int k=1;k<=kk;k++)                {                    if(a[i]==b[j])                    {                        dp[i][j][k][1]=max(dp[i-1][j-1][k-1][0]+1,dp[i][j][k][1]);                        dp[i][j][k][1]=max(dp[i-1][j-1][k][1]+1,dp[i][j][k][1]);                    }                    dp[i][j][k][0]=max(dp[i][j][k][1],dp[i][j][k][0]);                    dp[i][j][k][0]=max(dp[i][j-1][k][0],dp[i][j][k][0]);                    dp[i][j][k][0]=max(dp[i-1][j-1][k][0],dp[i][j][k][0]);                    dp[i][j][k][0]=max(dp[i-1][j][k][0],dp[i][j][k][0]);                }            }        }        printf("%d\n",max(dp[n][m][kk][0],dp[n][m][kk][1]));    }}










0 0
原创粉丝点击