博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aizu - ALDS1_10_C Longest Common Subsequence 动态规划
阅读量:3904 次
发布时间:2019-05-23

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

For given two sequences XX and YY , a sequence ZZ is a common subsequence of XX and YY if ZZ is a subsequence of both XX and YY . For example, if X={a,b,c,b,d,a,b}X={a,b,c,b,d,a,b} and Y={b,d,c,a,b,a}Y={b,d,c,a,b,a} , the sequence {b,c,a}{b,c,a} is a common subsequence of both XX and YY . On the other hand, the sequence {b,c,a}{b,c,a} is not a longest common subsequence (LCS) of XX and YY , since it has length 3 and the sequence {b,c,b,a}{b,c,b,a} , which is also common to both XX and YY , has length 4. The sequence {b,c,b,a}{b,c,b,a} is an LCS of XX and YY , since there is no common subsequence of length 5 or greater.

Write a program which finds the length of LCS of given two sequences XX and YY . The sequence consists of alphabetical characters.

Input

The input consists of multiple datasets. In the first line, an integer qq which is the number of datasets is given. In the following 2×q2×q lines, each dataset which consists of the two sequences XX and YY are given.

Output

For each dataset, print the length of LCS of XX and YY in a line.

Constraints

  • 1≤q≤1501≤q≤150
  • 1≤1≤ length of XX and YY ≤1,000≤1,000
  • q≤20q≤20 if the dataset includes a sequence whose length is more than 100

Sample Input 1

3abcbdabbdcabaabcabcabcbc

Sample Output 1

432

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

求最长公共子序列。 。 。

设有两个子串X,Y。 。。开一个数组dp[i][j],表示i行j列的最长公共子序列。

当i=0||j=0,dp[i][j]=0;

当xi= yj时,dp[i][j]=dp[i-1][j-1]+1;

当xi!=yj时,  dp[i][j]=max(dp[i][j-1],dp[i-1][j]) ;

代码如下:

 

#include 
#include
#include
#include
using namespace std;const int maxn=1005;int q;char a[maxn],b[maxn];int dp[maxn][maxn];void init(){ memset (dp,0,sizeof(dp));}int main(){ scanf("%d",&q); while (q--) { init(); scanf("%s\n%s",a,b); int len1=strlen(a),len2=strlen(b); for (int i=0;i

 

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

你可能感兴趣的文章
技术的热门度曲线
查看>>
Java数据库操作(JDBC)——eclipse连接oracle11g教程
查看>>
Java_JDBC_MySQL学习笔记
查看>>
Java数据库连接池 学习笔记
查看>>
Servlet & Jsp Web——Servlet开发(一)
查看>>
web服务器基本原理及Tomcat配置
查看>>
MyEclipse 2017配置Tomcat8
查看>>
HTTP协议简介
查看>>
VISIO 2010,不规则封闭图形填充方法
查看>>
双目立体视觉的数学原理
查看>>
特征值和特征向量
查看>>
AVR(Mega32)读写内部EEPROM
查看>>
牛人主页(主页有很多论文代码)
查看>>
博士学位在就业时有没有用?
查看>>
从一个男人身上看出他的修养和抱负
查看>>
免费编程入门教程资源推荐搜集,分享给想开始学习程序开发的同学
查看>>
5分钟学会用鼠标快速手绘数学公式的两种方法
查看>>
分享一些OpenCV实现立体视觉的经验
查看>>
施一公:优秀博士如何养成(全文) 清华大学演讲
查看>>
怎样做研究
查看>>