Common Subsequence(最长公共子序列)
来源:互联网 发布:淘宝发布图片规则 编辑:程序博客网 时间:2024/06/11 16:27
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
状态转移方程:若a[i] = b[j] F[i][j] = F[i-1][j-1] + 1若a[i] != b[j] F[i][j] = max(F[i-1][j-1],F[i-1][j],F[i][j-1])#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define num 1000int cnt[num][num];int max(int a, int b, int c){ int t = a > b ? a : b; return c > t ? c : t;}int main(){ char str1[num], str2[num]; int i, j, len1, len2; while (scanf("%s %s", str1, str2) != EOF) { len1 = strlen(str1); len2 = strlen(str2); memset(cnt,0,sizeof(cnt)); for (i = 1; i <= len1; ++ i) { for (j = 1; j <= len2; ++ j) { if (str1[i-1] == str2[j-1]) { cnt[i][j] = cnt[i-1][j-1] + 1; } else { cnt[i][j] = max(cnt[i-1][j-1], cnt[i-1][j], cnt[i][j-1]); } } } printf("%d\n", cnt[len1][len2]); memset(str1,0,sizeof(str1)); memset(str2,0,sizeof(str2)); } return 0;}
0 0
- 最长公共子序列Longest Common Subsequence
- ZJU1733 Common Subsequence - 最长公共子序列
- Common Subsequence hdu1159 最长公共子序列
- HDU Common Subsequence 最长公共子序列
- poj Common Subsequence 最长公共子序列
- POJ1458 Common Subsequence 【最长公共子序列】
- 最长公共子序列:HDU1159 Common Subsequence
- Common Subsequence(最长公共子序列)
- 最长公共子序列Common Subsequence POJ1458
- 最长公共子序列 (longest common subsequence)
- HDU1159 Common Subsequence 最长公共子序列
- Common Subsequence-最长公共子序列
- 最长公共子序列(Longest Common Subsequence)
- HDU Common Subsequence(最长公共子序列)
- POJ1458 Common Subsequence 最长公共子序列
- Common Subsequence(最长公共子序列)
- Longest Common Subsequence 最长公共子序列
- poj1458-Common Subsequence(最长公共子序列)
- gitlab安装手册官方修正版(转)
- 求矩阵的局部极大值(15)
- 欧几里得算法与扩展欧几里得算法
- MainLoop.c (2)函数SendQuery 和 HandleSlashCmds
- alsa分析:alsa的那些配置文件2
- Common Subsequence(最长公共子序列)
- 关于unity动态添加游戏对象问题,如何动态添加一个菜单【二】
- Wireshark学习FTP流程
- PAT 1010. 一元多项式求导 (25)
- Mysql关键字 Mysql保留字列表 Mysql字段名
- UVa 10935 - Throwing cards away I
- 杂记asla-lib库函数snd_pcm_open打开流程
- Vivado开发工具熟悉之IP Intergrator(Generate Block Design)
- 【poj 3056】The Bavarian Beer Party (区间DP+最大匹配不交叉的简单算法)