题目链接:

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. &quot;转成"

    在java中这样转 StringEscapeUtils.unescapeHtml(soapResponseData); 在js中这样转 str.replace(""",& ...

  2. hdu 1043 A*

    http://www.cnblogs.com/183zyz/archive/2011/08/12/2135827.html #include<stdio.h> #define N 3630 ...

  3. [NOIP1999] 提高组 洛谷P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  4. BZOJ1916: [Usaco2010 Open]冲浪

    n<=50000个点m<=150000的带边权DAG,保证1入度0,n出度0,其他点入度出度均不为0,求:从一号点开始到n,期间有可能K<=10次随机选边走,最坏情况下总边权多少. ...

  5. Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E

    D. Valid BFS? time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. 【Java源码】树-概述

    树的基本术语 结点(node)由数据元素以及指向子树的地址构成. 若 X 结点有子树,则子树的根结点称为 X 的孩子(child)结点,相应地, X 称为其孩子的双亲(parents)结点,又称父母结 ...

  7. navicat 无法连接到腾讯云Mysql

    远程连接进入服务器 远程连接进入服务器之后,输入命令mysql -u root -p 之后输入mysql密码,进入mysql 命令环境 设置开启远程登录 GRANT ALL PRIVILEGES ON ...

  8. Firefox下td用display控制页面导致页面变形

    Firefox下table的td元素假设使用了display:'block'会使得table变形.原因是block会将对象强制作为块对象呈递,为对象之后加入新行,所以并不适合td,改成display: ...

  9. [Rust] Load a WebAssembly Function Written in Rust and Invoke it from JavaScript

    In this lesson we are going to setup a project from scratch by introducing the JavaScript snippet to ...

  10. poj2481 Cows

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...