CF#358 D. Alyona and Strings DP
D. Alyona and Strings
题意
给出两个字符串s,t,让找出最长的k个在s,t不相交的公共子串。
思路
看了好几个题解才搞懂。
代码中有注释
代码
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
char s[N],t[N],k;
int dp[1010][1010][11][2];
/*
终于搞明白了
dp[i][j][k][0]表示的是s的前i个字符,t的前j个字符,选择k个公共子串的最大值(不一定包括i,j)
dp[i][j][k][1]表示的是s的前i个字符,t的前j个字符,选择k个公共子串最后是i,j的最大值
dp[lens][lent][k][0]就是题目要求的答案。
当s[i]==t[j]的时候dp[i][j][k][1]才存在(不为0)
*/
int main()
{
int lens,lent;
scanf("%d%d%d%s%s",&lens,&lent,&k,s+1,t+1);
for(int i=1; i<=lens; i++)
{
for(int j=1; j<=lent; j++)
{
for(int l=1; l<=k; l++)
{
if(s[i]==t[j])//只有s[i]==t[j]的时候才有dp[i][j][l][1]
dp[i][j][l][1]=max(dp[i-1][j-1][l][1]+1,dp[i-1][j-1][l-1][0]+1);
/*可以沿用之前的第l个公共子串,或者让i,j作为第l个子串的第一位*/
dp[i][j][l][0]=max(dp[i-1][j][l][0],max(dp[i][j-1][l][0],dp[i][j][l][1]));
}
}
}
printf("%d\n",dp[lens][lent][k][0]);
return 0;
}
/*
*/
CF#358 D. Alyona and Strings DP的更多相关文章
- 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 ...
- codeforces 682D D. Alyona and Strings(dp)
题目链接: D. Alyona and Strings time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 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 time limit per test2 seconds memory limit per test256 megabytes input ...
- CodeForces 682D Alyona and Strings (四维DP)
Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...
- D. Alyona and Strings 解析(思維、DP)
Codeforce 682 D. Alyona and Strings 解析(思維.DP) 今天我們來看看CF682D 題目連結 題目 略,請直接看原題. 前言 a @copyright petjel ...
- Codeforces 682D Alyona and Strings(DP)
题目大概说给两个字符串s和t,然后要求一个包含k个字符串的序列,而这个序列是两个字符串的公共子序列,问这个序列包含的字符串的总长最多是多少. 如果用DP解,考虑到问题的规模,自然这么表示状态: dp[ ...
- 【31.58%】【codeforces 682D】Alyona and Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
随机推荐
- D. Minimax Problem Codeforces 1288D binary_search+二进制
题目大意:n*m的矩阵中,找到两行数,可以形成两个一维数组,数组1的位置i和数组2的位置i去最大构成新数组b的元素b[i],最终目的要使数组b中最小的数尽可能的大 题解: m的范围是(1,8),比较小 ...
- First Training
B B - Local Extrema CodeForces - 888A You are given an array a. Some element of this array ai is a l ...
- css特效sh
1 opacity=0.5: 透明度 2 选择器 .btn1:ho ...
- Springboot:员工管理之添加员工(十(7))
构建员工添加请求 com\springboot\controller\EmployeeController.java /*调转到员工添加页 携带部门信息 restful风格*/ @GetMapping ...
- Springboot:员工管理之环境准备(十(1))
1:静态资源 下载静态资源:https://files.cnblogs.com/files/applesnt/ztzy.zip 项目下载:https://files.cnblogs.com/files ...
- APT32入侵我国,试图窃取COVID-19相关情报
新闻一篇: 一直以来,APT32都以东南亚为攻击目标,并且是近几年来针对中国大陆进行攻击活动最活跃的APT攻击组织,没有之一.此次再将目标对准中国,与新冠疫情离不开关系. 4月22日,Fireye发布 ...
- Redis 5.0.9 安装
目录 系统环境 系统版本 内核版本 安装步骤 安装 gcc 依赖 下载 Redis 解压 Redis 切换到 redis 解压目录下,执行编译 指定目录安装 启动 Redis 服务 最后 系统环境 系 ...
- pytorch 中的LSTM模块
- pytorch中的前项计算和反向传播
前项计算1 import torch # (3*(x+2)^2)/4 #grad_fn 保留计算的过程 x = torch.ones([2,2],requires_grad=True) print(x ...
- Java 反射 -- 获取泛型类型
先写一个类: public class Demo03 { public void test01(Map<String, User> map, List<User> list) ...