博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1458 Common Subsequence 【最长公共子序列】
阅读量:4314 次
发布时间:2019-06-06

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

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37614   Accepted: 15058

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

#include 
#include
#define maxn 1000char str1[maxn], str2[maxn];int dp[maxn][maxn];int max(int a, int b){ return a > b ?

a : b; } int LCS() { int ans = 0; for(int i = 1, j; str1[i]; ++i){ for(j = 1; str2[j]; ++j){ if(str1[i] == str2[j]){ dp[i][j] = dp[i-1][j-1] + 1; }else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if(dp[i][j] > ans) ans = dp[i][j]; } } return ans; } int main() { while(scanf("%s%s", str1 + 1, str2 + 1) == 2){ printf("%d\n", LCS()); } return 0; }

转载于:https://www.cnblogs.com/mengfanrong/p/5088825.html

你可能感兴趣的文章
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>
MySQL 5.1参考手册
查看>>
TensorFlow安装流程(GPU加速)
查看>>
OpenStack的容器服务体验
查看>>
BZOJ1443: [JSOI2009]游戏Game
查看>>
【BZOJ 4059】 (分治暴力|扫描线+线段树)
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>
linux 内核的优化
查看>>
Spark笔记之DataFrameNaFunctions
查看>>
Oracle 时间函数 (转)
查看>>
近端梯度算法(Proximal Gradient Descent)
查看>>
DRM-内容数据版权加密保护技术学习(中):License预发放实现 (转)
查看>>
TCP与UDP协议
查看>>
php上传文件如何保证上传文件不被改变或者乱码
查看>>
目录遍历代码
查看>>