博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(最长公共子序列 暴力) Common Subsequence (poj 1458)
阅读量:6085 次
发布时间:2019-06-20

本文共 1791 字,大约阅读时间需要 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, xij = 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
#include 
#include
#include
#include
#include
using namespace std;#define met(a,b) (memset(a,b,sizeof(a)))#define N 1100#define INF 0xffffffchar s1[N], s2[N];int dp[N][N];int main(){ while(scanf("%s%s", s1, s2)!=EOF) { int i, j, len1=strlen(s1), len2=strlen(s2); met(dp, 0); ///要考虑下下标越界的问题 for(i=1; i<=len1; i++) for(j=1; j<=len2; j++) { if(s1[i-1]==s2[j-1]) dp[i][j] = dp[i-1][j-1] + 1; else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } printf("%d\n", dp[len1][len2]); } return 0;}

 

 

转载于:https://www.cnblogs.com/YY56/p/5444205.html

你可能感兴趣的文章
Internet Message Access Protocol --- IMAP协议
查看>>
Linux 获取文件夹下的所有文件
查看>>
对 Sea.js 进行配置(一) seajs.config
查看>>
第六周
查看>>
解释一下 P/NP/NP-Complete/NP-Hard 等问题
查看>>
javafx for android or ios ?
查看>>
微软职位内部推荐-Senior Software Engineer II-Sharepoint
查看>>
sql 字符串操作
查看>>
【转】Android布局优化之ViewStub
查看>>
网络安全管理技术作业-SNMP实验报告
查看>>
根据Uri获取文件的绝对路径
查看>>
Flutter 插件开发:以微信SDK为例
查看>>
.NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理?...
查看>>
边缘控制平面Ambassador全解读
查看>>
Windows Phone 7 利用计时器DispatcherTimer创建时钟
查看>>
程序员最喜爱的12个Android应用开发框架二(转)
查看>>
vim学习与理解
查看>>
DIRECTSHOW在VS2005中PVOID64问题和配置问题
查看>>
MapReduce的模式,算法以及用例
查看>>
《Advanced Linux Programming》读书笔记(1)
查看>>