HZAU 17:LCS
17: LCS
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 184 Solved: 43
[Submit][Status][Web Board]
Description
Giving two strings consists of only lowercase letters, find the LCS(Longest Common Subsequence) whose all partition are not less than k in length.
Input
There are multiple test cases. In each test case, each of the first two lines is a string(length is less than 2100). The third line is a positive integer k. The input will end by EOF.
Output
For each test case, output the length of the two strings’ LCS.
Sample Input
abxccdef
abcxcdef
3
abccdef
abcdef
3
Sample Output
4
6
题目分析:设两个字符串分别为p和q,定义状态dp(i,j)表示p的前缀p(1~i)和q的前缀q(1~j)的满足题目要求的LCS的长度,定义g(i,j)表示满足题目要求的并且结尾字符为p(i)或q(j)(显然,这要求p(i)=q(j))的LCS的长度。
的长度。则状态转移方程为:dp(i,j)=max(dp(i-1,j),dp(i,j-1),g(i,j))。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; int k;
char p[2105];
char q[2105];
int f[2105][2105];
int g[2105][2105];
int dp[2105][2105]; int main()
{
//freopen("F.in","r",stdin);
while(~scanf("%s%s%d",p+1,q+1,&k))
{
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
memset(dp,0,sizeof(dp));
int n=strlen(p+1);
int m=strlen(q+1);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(p[i]==q[j]) f[i][j]=f[i-1][j-1]+1;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(f[i][j]>=k){
g[i][j]=max(g[i][j],dp[i-k][j-k]+k);
if(f[i][j]>k)
g[i][j]=max(g[i][j],g[i-1][j-1]+1);
}
dp[i][j]=max(max(dp[i-1][j],dp[i][j-1]),g[i][j]);
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}
HZAU 17:LCS的更多相关文章
- 最长公共子序列LCS
LCS:给出两个序列S1和S2,求出的这两个序列的最大公共部分S3就是就是S1和S2的最长公共子序列了.公共部分 必须是以相同的顺序出现,但是不必要是连续的. LCS具有最优子结构,且满足重叠子问题的 ...
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- 解题报告:hdu1159 common consequence LCS裸题
2017-09-02 17:07:42 writer:pprp 通过这个题温习了一下刚学的LCS 代码如下: /* @theme:hdu1159 @writer:pprp @begin:17:01 @ ...
- LIS和LCS LCIS
首先介绍一下LIS和LCS的DP解法O(N^2) LCS:两个有序序列a和b,求他们公共子序列的最大长度 我们定义一个数组DP[i][j],表示的是a的前i项和b的前j项的最大公共子序列的长度,那么由 ...
- Codeforces Gym100735 G.LCS Revised (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
G.LCS Revised The longest common subsequence is a well known DP problem: given two strings A and B ...
- nyoj 37-回文字符串(reverse, 动态规划, lcs)
37-回文字符串 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:10 submit:17 题目描述: 所谓回文字符串,就是一个字符串,从左到右读和从 ...
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- .NET平台开源项目速览(17)FluentConsole让你的控制台酷起来
从该系列的第一篇文章 .NET平台开源项目速览(1)SharpConfig配置文件读写组件 开始,不知不觉已经到第17篇了.每一次我们都是介绍一个小巧甚至微不足道的.NET平台的开源软件,或者学习,或 ...
- Centos 6.6 下搭建php5.2.17+Zend Optimizer3.3.9+Jexus环境
(为何安装php5.2.17这个版本 因为phpweb这个程序用到了Zend Optimizer3.3.9 这个东东已经停止更新了 最高支持5.2版本的php 所以就有了一晚上填坑的自己和总结了这篇文 ...
随机推荐
- hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏
as Scott Meyers said in his book Effective STL, "My advice on choosing among the sorting algori ...
- CSS实现图片变灰色及透明度
[图片变灰] 每当遇到哀悼日,很多网站快速变灰色,来看看实现方式吧: 方式一,仅支持ie) html{filter:progid:DXImageTransform.Microsoft.BasicIma ...
- SharePoint 2013 开发——开发并部署第一个APP
博客地址:http://blog.csdn.net/FoxDave 本篇我们开始对开发APP应用程序进行了解. 本篇基于本地SharePoint环境(如果是Office 365的话会方便许多),需 ...
- C# WebBrowser NativeMethods
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...
- PHP time() 函数
定义和用法 time() 函数返回当前时间的 Unix 时间戳. 语法 time(void) 参数 描述 void 可选. 说明 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 ...
- C语言:typedef 跟 define 的区别
typedef (int*) pINT1;以及下面这行:#define pINT2 int* pINT1 a,b; 与pINT2 a,b; 定义的a,b 有差别吗 回答: typedef作为类型定义关 ...
- CPU MPU MCU SOC SOPC关系及区别
在嵌入式开过程,会经常接触到一些缩写术语概念,这些概念在嵌入式行业中使用率非常高,下面我们就解释一下这些概念之间的关系和区别: 1.CPU(Central Processing Unit),是一台计算 ...
- mysql批量写入
MySQL批量写入语法是: INSERT INTO table (field1,field2,field3) VALUES (“a”,”b”,”c”), (“a1”,”b1”,”c1”),(“a2”, ...
- php大力力 [036节] 后台系统的登录页面界面做完啦
php大力力 [036节] 后台系统的登录页面界面做完啦 我认为做的不错,我就先不上截图啦 要你的祝福 分布注册 Twitter Login Or Signup Form 藤藤每日一练——172个Ic ...
- Sticks_dfs
Description George took sticks of the same length and cut them randomly until all parts became at mo ...