博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ACM 最大公共子序列
阅读量:4974 次
发布时间:2019-06-12

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

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
#include
using namespace std;int const N=1000;int save[N][N];char str1[N],str2[N];int max(int x,int y){ return x>y?x:y;}int maxlen(char *str1,char *str2){ int n1=strlen(str1),n2=strlen(str2),i,j; for(i=0;i<=max(n1,n2);i++) save[i][0]=save[0][i]=0; for(i=1;i<=n1;i++) for(j=1;j<=n2;j++) //以下从str1[0]和str2[0]开始比较 save[i][j]=(str1[i-1]==str2[j-1] ? save[i-1][j-1]+1 : max(save[i-1][j],save[i][j-1])); return save[n1][n2];}int main(){ while(scanf("%s%s",str1,str2)!=EOF) { memset(save,0,sizeof(save)); cout<
<
 

  

 

转载于:https://www.cnblogs.com/xinxiangqing/p/4730947.html

你可能感兴趣的文章
数据结构实习 problem L 由二叉树的中序层序重建二叉树
查看>>
VS中展开和折叠代码
查看>>
如何确定VS编译器版本
查看>>
设置PL/SQL 快捷键
查看>>
个人阅读作业7
查看>>
转载:深入浅出Zookeeper
查看>>
GMA Round 1 新程序
查看>>
node anyproxy ssi简易支持
查看>>
编译预处理指令:文件包含指令、宏定义指令、条件编译指令
查看>>
PHP函数 ------ ctype_alnum
查看>>
网站安全
查看>>
WS-Addressing 初探
查看>>
.NET+模块编排+数据库操作类的封装+分层架构+实体类+Ajax.net+Athem.NET+javascript+Activex组件+用户权限等...
查看>>
Markdown不常见功能
查看>>
(二)NUnit单元测试心得
查看>>
hdu_2604Queuing(快速幂矩阵)
查看>>
frame.bounds和center
查看>>
HDU 1102 Constructing Roads
查看>>
android StaticLayout参数解释
查看>>
多线程之ThreadLocal类
查看>>