【31.58%】【codeforces 682D】Alyona and Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

【题解】
设
f[i][j][k][0]表示s的前i个字母,t的前j个字母里面符合要求的公共序列为k个,且s[i]和t[j]不是第k个公共序列的最后一个元素的最长序列长度;
f[i][j][k][1]表示s的前i个字母,t的前j个字母里面符合要求的公共序列为k个,且s[i]和t[j]是第k个公共序列的最后一个元素的最长序列长度;
边界:
f[i][j][k][0..1]= 0;
f[1][1][1][1] = (s[1]==t[1]?1:0);
根据s[i]是不是等于s[j]。转移一下就可以了。
看代码吧。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000 + 20;
const int MAXK = 15;
char s[MAXN],t[MAXN];
int n, m, k;
int dp[MAXN][MAXN][MAXK][2];
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d%d%d", &n, &m, &k);
scanf("%s", s + 1);
scanf("%s", t + 1);
if (s[1] == t[1])
dp[1][1][1][1] = 1;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
for (int l = 1; l <= k; l++)
{
if (i == 1 && j == 1)
continue;
if (i == 1)
{
if (s[1] == t[j])
dp[1][j][1][1] = 1;
dp[1][j][1][0] = dp[1][j - 1][1][0];
if (s[1] == t[j - 1])
dp[1][j][1][0] = max(dp[1][j][1][0],dp[1][j - 1][1][1]);
}
else
if (j == 1)
{
if (s[i] == t[1])
dp[i][1][1][1] = 1;
dp[i][1][1][0] = dp[i-1][1][1][0];
if (s[i-1] == t[1])
dp[i][1][1][0] = max(dp[i][1][1][0], dp[i-1][1][1][1]);
}
else
{
if (s[i] == t[j])
{
dp[i][j][l][1] = dp[i - 1][j - 1][l - 1][0]+1;
if (s[i - 1] == t[j - 1])
{
dp[i][j][l][1] = max(dp[i][j][l][1], dp[i - 1][j - 1][l - 1][1] + 1);
dp[i][j][l][1] = max(dp[i][j][l][1], dp[i - 1][j - 1][l][1] + 1);
}
}
dp[i][j][l][0] = max(dp[i - 1][j][l][0], dp[i][j - 1][l][0]);
dp[i][j][l][0] = max(dp[i][j][l][0], dp[i - 1][j - 1][l][0]);
if (s[i] == t[j - 1])
dp[i][j][l][0] = max(dp[i][j][l][0], dp[i][j - 1][l][1]);
if (s[i - 1] == t[j])
dp[i][j][l][0] = max(dp[i][j][l][0], dp[i - 1][j][l][1]);
if (s[i - 1] == s[j - 1])
dp[i][j][l][0] = max(dp[i][j][l][0], dp[i - 1][j - 1][l][1]);
}
}
printf("%d\n", max(dp[n][m][k][0], dp[n][m][k][1]));
return 0;
}
【31.58%】【codeforces 682D】Alyona and Strings的更多相关文章
- codeforces 682D D. Alyona and Strings(dp)
题目链接: D. Alyona and Strings time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- Codeforces 682 D. Alyona and Strings (dp)
题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...
- 【31.72%】【codeforces 604B】More Cowbell
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【21.58%】【codeforces 746D】Green and Black Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【58.33%】【codeforces 747B】Mammoth's Genome Decoding
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
随机推荐
- 硬件——STM32 , 软件框架
单片机应用程序的框架大概有三种: 1,简单的前后台顺序执行程序. 2,时间片轮询法. 3,应用操作系统. 下面我们主要来讲解时间片轮询法: 在这里我们先介绍一下定时器的复用功能.就是使用1个定时器,可 ...
- Loadrunner--web_find和web_reg_find的用法和区别
一.web_find()函数 该函数的作用是“在页面中查找相应的内容”,常用参数及含义如下: web_find("web_find", //定义该查找函数的名称 "Rig ...
- jemter--录制的脚本设置循环次数不起作用
以下是比较jmeter线程组中设置循环次数和循环控制器中设置循环次数的区别 1.jmeter生成的脚本没有step1(循环控制器)控制器,故循环在线程组中设置 2.badboy录制的脚本有setp ...
- 微服务实战(六):选择微服务部署策略 - DockOne.io
原文:微服务实战(六):选择微服务部署策略 - DockOne.io [编者的话]这篇博客是用微服务建应用的第六篇,第一篇介绍了微服务架构模板,并且讨论了使用微服务的优缺点.随后的文章讨论了微服务不同 ...
- 【Codeforces Round #443 (Div. 2) B】Table Tennis
[链接] 我是链接,点我呀:) [题意] n个人站在一排. 每次第一个人和第二个人打架. 输的人跑到队列的尾巴去. 然后赢的人继续在队首.和第三个人打. 谁会先赢K次. [题解] 会发现,一轮之后就一 ...
- Java FutureTask Example Program(Java FutureTask例子)
Sometime back I wrote a post about Java Callable Future interfaces that we can use to get the concur ...
- docker 第一课 —— 从容器到 docker
1. 容器的概念 一种虚拟化的解决方案 与虚拟机所不同的是,虚拟机通过中间层,将一台或多台独立的机器虚拟运行于物理硬件之上: 而容器是直接运行于操作系统内核之上的用户空间: 基于上述,容器虚拟化也被称 ...
- (转)Windows2008优化IIS7.5支持10万个同时请求的配置方法
通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅. 在运行中cmd后,输入:C:\Window ...
- 【习题 5-10 UVA-1597】Searching the Web
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串 ...
- or小计
1.使用or的时候,必须养成两边添加括号,否则结果完全不一样. 2.or条件如果复杂的情况下,可以适当考虑union all改写.