题目链接:

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 nmk (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

题意:

给两个字符串,这两个字符串有k个字串相同,求这k个字串长度和最大是多少;

思路:

dp[i][j][k][end]表示s的前i个和t的前j个有k个字串相同,的最长长度,end==0表示这个点还不是第k个字串的结尾,1表示是第k个子串的结尾;
然后就是转移了,具体的看代码; AC代码:
#include <bits/stdc++.h>
/*#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+;
const int maxn=; int dp[][][][];
char s[],t[];
int main()
{
int n,m,k;
read(n);read(m);read(k);
scanf("%s",s+);scanf("%s",t+);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(s[i]==t[j])
{
for(int x=;x<=k;++x)
{
dp[i][j][x][]=max(dp[i-][j-][x][],dp[i-][j-][x-][])+;
}
}
for(int x=;x<=k;++x)
{
dp[i][j][x][]=max(max(dp[i-][j][x][],dp[i][j-][x][]),max(dp[i][j][x][],dp[i-][j-][x][]));
}
}
}
printf("%d\n",dp[n][m][k][]);
return ;
}

codeforces 682D D. Alyona and Strings(dp)的更多相关文章

  1. 【31.58%】【codeforces 682D】Alyona and Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. Codeforces 682 D. Alyona and Strings (dp)

    题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...

  3. Codeforces Round #358 (Div. 2) D. Alyona and Strings dp

    D. Alyona and Strings 题目连接: http://www.codeforces.com/contest/682/problem/D Description After return ...

  4. CF#358 D. Alyona and Strings DP

    D. Alyona and Strings 题意 给出两个字符串s,t,让找出最长的k个在s,t不相交的公共子串. 思路 看了好几个题解才搞懂. 代码中有注释 代码 #include<bits/ ...

  5. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

  6. Codeforces Round #358 (Div. 2) D. Alyona and Strings 字符串dp

    题目链接: 题目 D. Alyona and Strings time limit per test2 seconds memory limit per test256 megabytes input ...

  7. D. Alyona and Strings 解析(思維、DP)

    Codeforce 682 D. Alyona and Strings 解析(思維.DP) 今天我們來看看CF682D 題目連結 題目 略,請直接看原題. 前言 a @copyright petjel ...

  8. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  9. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

随机推荐

  1. java中如何将string转化成long

  2. 安装weblogic时,运行configure.cmd报错、闪退、无法创建域

    直接运行configure.cmd时在jar包加载完成时,不提示创建域的过程,而是直接退出程序 命令行: cd /d F:\00uep_rfs\wls1212_dev\wls12120 切换至解压路径 ...

  3. centos7安装rlwrap

    http://utopia.knoware.nl/~hlub/uck/rlwrap/ 下载rlwrap-0.42.tar.gz 找到centos7 安装的iso中的 Packages的 ncurses ...

  4. HDU 4821 字符串hash

    题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...

  5. 病毒的侵扰和再侵扰两道AC自动机的应用

    HDU2896 病毒的侵扰 http://vjudge.net/problem/viewProblem.action?id=16404 题目大意: 记录每个病毒的编号,并给出一些网站的源码,分别输出网 ...

  6. JS获取服务器时间并且计算距离当前指定时间差的函数

    项目中遇到了从服务器获取时间,现在记录一下方便以后查询: 1.后台代码:(创建一个date对象并以JSON的形式返回去) // 获取服务器时间 public String getNowServerTi ...

  7. hihoCoder #1055 : 刷油漆 [ 树形dp ]

    传送门 结果:Accepted     提交时间:2015-05-11 10:36:08 #1055 : 刷油漆 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到 ...

  8. Intent和IntentFilter简介

    Intent和IntentFilter简介 Intent和IntentFilter简介 意图Intent分类: 显式意图:利用class找到对方,在同一个应用程序类可以方便使用,但是在不同的应用程序无 ...

  9. hdu1875kruskal简单应用。

    标记是dificulty 2,水,开始kruskal时练手题,只需开始时数据处理下,不符合要求的边不要,要理解并查集和Kruskal,就简单了,判断下是否联通图,(只需在记加入有效边时候统计连通分支数 ...

  10. 导师高茂源:用CODEX创新方法破解西方创新“秘密”(转)

    高茂源,“CODEX创新体系”的创立者,精一学社的创业导师.“CODEX”是Copy.Optimize.Dimension.Ecosystem.Extra五个单词的缩写,该体系精炼了现在世界上流行的创 ...