codeforces 682D D. Alyona and Strings(dp)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
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.
3 2 2
abc
ab
2
9 12 4
bbaaababb
abbbabbaaaba
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)的更多相关文章
- 【31.58%】【codeforces 682D】Alyona and Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 682 D. Alyona and Strings (dp)
题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...
- 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 ...
- CF#358 D. Alyona and Strings DP
D. Alyona and Strings 题意 给出两个字符串s,t,让找出最长的k个在s,t不相交的公共子串. 思路 看了好几个题解才搞懂. 代码中有注释 代码 #include<bits/ ...
- CodeForces 682D Alyona and Strings (四维DP)
Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...
- 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 ...
- D. Alyona and Strings 解析(思維、DP)
Codeforce 682 D. Alyona and Strings 解析(思維.DP) 今天我們來看看CF682D 題目連結 題目 略,請直接看原題. 前言 a @copyright petjel ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
随机推荐
- kubernetes集群新增node
kubernetes集群要新增node,首先要配置ssh免密登陆 root@ht:/etc/ansible# ssh-copy-id 172.18.196.6 /usr/bin/ssh-copy-id ...
- 【HDOJ6305】RMQ Similar Sequence(笛卡尔树)
题意: 给定一个数组a,现在存在一个数组b,其元素值在[0,1]随机生成 若对于a,b,任意rmq问题的最值出现在同一个数组中的位置,则数组b的价值为∑b[i],否则为0,求数组b的期望价值 n< ...
- Visual Studio Code Edit
微软的跨平台编辑器~~ 下载地址(官网):https://code.visualstudio.com/ 下载地址(网盘):http://pan.baidu.com/s/1ntLy8Tr 使用技巧: c ...
- 修改textfield的占位符颜色
[self.titleField setValue:UIColorFromHEXWithAlpha(0x999999, 1) forKeyPath:@"_placeholderLabel.t ...
- Linux下增加User及添加sudo权限
运行adduser username 会默认建立同名的user,group,同时会要求输入用户密码及一些属性,完成之后OK. sudo chmod +w /etc/sudoers vi /etc/su ...
- python学习之-- Mysql 基础知识
数据库介绍及MYSQL基础操作了解 关系型数据库(RDBMS)是按照数据结构来组织,存储和管理数据的仓库.特点:1:数据以表格的形式出现2:每行为各种记录名称3:每列为记录名称所对应的数据域4:许多的 ...
- 【Java源码】集合类-ArrayList
一.类继承关系 public class ArrayList<E> extends AbstractList<E> implements List<E>, Rand ...
- Two Paths--cf14D(树的直径)
题目链接:http://codeforces.com/problemset/problem/14/D D. Two Paths time limit per test 2 seconds memory ...
- Java课堂测试--实现ATM的基本操作体会
9月20的周四的Java课堂第一节课上就是有关于实现ATM的考试内容,在实现的过程中我了解到自己本身还是有很多的不足之处,例如在实现工程方面的相似性上面还有些许就的欠缺,再者就是回宿舍拿电源的原因导致 ...
- Python 点滴 I
[为什么使用Python] 1. 软件质量: Python更注重软件质量,一致性,可维护性 2. 开发效率: 相比C/C++/Java这些编译/静态语言,无需编译及链接步骤,Python所须要 ...