博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1458-Common Subsequence(线性dp/LCS)
阅读量:6496 次
发布时间:2019-06-24

本文共 1737 字,大约阅读时间需要 5 分钟。

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39009   Accepted: 15713

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, x
ij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc         abfcabprogramming    contest abcd           mnp

Sample Output

420
最长公共子序列问题。
二维dp。dp[i][j]代表字符串s的前i个字符与字符串t的前j个字符的最长公共子序列的长度。dp[0][0]=0;
if(s[i]==t[j])dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);//从前状态取最大
 
#include 
#include
#include
using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 510;#define LL long longint dp[maxn][maxn]={0};char s[maxn],t[maxn];int main(){ while(scanf("%s %s",s,t)!=EOF) { int ls=strlen(s),lt=strlen(t); for(int i=1;i<=ls;i++) for(int j=1;j<=lt;j++) dp[i][j]=s[i-1]==t[j-1]?dp[i-1][j-1]+1:max(dp[i-1][j],dp[i][j-1]); printf("%d\n",dp[ls][lt]); } return 0;}

转载地址:http://gncyo.baihongyu.com/

你可能感兴趣的文章
bzoj 2733 平衡树启发式合并
查看>>
sublime简书安装配置
查看>>
爱上MVC~Web.Config的Debug和Release版本介绍
查看>>
【转】那些年我们一起清除过的浮动
查看>>
python__高级 : 动态添加 对象属性, 类属性, 对象实例方法, 类静态方法, 类方法...
查看>>
NLog的介绍使用
查看>>
Haproxy+Rabbitmq中的问题
查看>>
字符串变量小议
查看>>
232. Implement Queue using Stacks
查看>>
Poj(1469),二分图最大匹配
查看>>
和菜鸟一起学linux之V4L2摄像头应用流程【转】
查看>>
spin_lock、spin_lock_irq、spin_lock_irqsave区别【转】
查看>>
删除 mac 垃圾桶内清除不掉的文件
查看>>
【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例
查看>>
/bin/bash^M: bad interpreter: No such file or dire
查看>>
python xml rpc
查看>>
Java设置以及获取JavaBean私有属性进阶
查看>>
db2表结构导出导入,数据库备份
查看>>
策略模式
查看>>
OrderOnline——项目概述
查看>>