博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj_1458 LCS problem F.最长上升公共子序列
阅读量:4699 次
发布时间:2019-06-09

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

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
 
分析:
d[i][j]表示a[1],a[2]……a[i] 和b[1],b[2]……b[i]的最长上升子序列长度.while(a[i]==a[j])  d[i][j]=d[i-1][j-1]+1;   else  d[i][j]=max{d[i-1][j],d[i][j-1]};     时间复杂度为o(n*m).
 
代码及简要分析:
1 #include
2 #include
3 #include
4 #include
//使用cin>>输入需要加上此头文件 5 using namespace std; 6 string a,b; 7 int dp[1000][1000]; 8 int max(int x,int y) 9 {10 if(x>=y)11 return x;12 else13 return y;14 }15 16 int main()17 {18 int i,j;19 while(cin>>a)20 {21 cin>>b;22 memset(dp,0,sizeof(dp));//ddp[i][j]为a[0]...a[i]和b[0]...b[j]的最长公共子序列的长度。23 for(i=0;i
View Code

 

 

转载于:https://www.cnblogs.com/x512149882/p/4738706.html

你可能感兴趣的文章
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
修改node节点名称
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
项目开发总结报告(GB8567——88)
查看>>
SSH加固
查看>>
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
pip 警告!The default format will switch to columns in the future
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>