Problem 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.
The program
input is from a text file. Each data set in the file contains two strings
representing the given sequences. The sequences are separated by any number of
white spaces. The input data are correct. 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 abfcab
programming contest
abcd mnp
 
Sample Output
4
2
0
 
 
 
 
关键点:要掌握问题的转化,关键点是要理解,假设序列 X={X1,X2...Xm}和Y={Y1,Y2,...Yn}的最长公共子序列为Z={Z1,Z2...Zk)
(1)若Xm = Yn 则 Zk=Xm=Yn ,且Zk-1 是Xm-1 和 Yn-1的最长公共子序列
(2)若Xm 不等于Yn,且Zk不等于Xm 则Z是Xm-1和Y的最长公共子序列
(3)若Xm不等于Yn,且Zk不等于Yn 则Z是X和Yn-1 的最长公共子序列
 

#include<stdio.h>
#include<string.h>
int c[500][500],lena,lenb;
int max(int a,int b){
if(a>=b)
return a;
else
return b;
}
int main(){
char a[500],b[500];
while(scanf("%s%s",a,b)==2){
lena = strlen(a);
lenb = strlen(b);
for(int i=0;i<lena;i++)
c[i][0]=0;
for(int j=0;j<lenb;j++)
c[0][j]=0;
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++){
if(a[i-1]==b[j-1])//这里需要注意下,不能忘记字符a[0],b[0]的比较,所以在计算的过程中需要计算到lena
c[i][j]=c[i-1][j-1]+1;
else
c[i][j]=max(c[i-1][j],c[i][j-1]);
}
printf("%d\n",c[lena][lenb]);
}
return 0;
}

 
 

Common Subsequence 最大公共子序列问题的更多相关文章

  1. 算法:Common Subsequence(动态规划 Java 最长子序列)

    Description A subsequence of a given sequence is the given sequence with some elements (possible non ...

  2. spoj Longest Common Substring (多串求最大公共子序列)

    题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...

  3. Common Subsequence Gym - 102307C 公共序列

    2019 ICPC Universidad Nacional de Colombia Programming Contest C D J   C. Common Subsequence 题意:给出长度 ...

  4. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  5. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  6. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  7. LCS最大公共子序列问题

    在生物应用中,经常需要比较两个(或多个)不同生物体的DNA, 例如:某种生物的DNA可能为S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA, 另一种生物的DNA可能为S2=GTCGTT ...

  8. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  9. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

随机推荐

  1. 用实现ajax读博客rss示例代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  2. 关于Cocos2d-x中节点和精灵的关系以及初始化

    1.每一个对象类都有一个自己public的一个create函数(等价于CREATE_FUNC),和init函数. 2.create函数返回的是自身的类型,init函数是在ceate函数被调用的时候自动 ...

  3. Semi-Supervised Classification with Graph Convolutional Networks

    Kipf, Thomas N., and Max Welling. "Semi-supervised classification with graph convolutional netw ...

  4. e684. 以多种格式打印

    A Book object is used when printing pages with different page formats. This example prints the first ...

  5. par函数的adj 参数- 控制文字的对齐方式

    adj 用来控制文字的对齐方式,取值范围为0到1,控制图片中x轴和y轴标签,标题,以及通过text 添加的文字的对齐方式 0表示左对齐,代码示例: par(adj = 0)plot(1:5, 1:5, ...

  6. TTreeView TTreeNodes TTreeNode

    TTreeView 填写 TTreeView 的内容一般是这样开始的(下图), 不过我觉得最好习惯用动态建立. 打个比方: 譬如 TreeView 是一个军营的"营部"! 这里会有 ...

  7. Ubuntu 12.04.3 X64 使用 NFS 作为文件共享存储方式 安装 Oracle11g RAC

    nfs-server 在 Ubuntu上可以选择 : nfs-kernel-server:如果在windows上,可以选择:haneWIN NFS Server nfs-client Ubuntu上使 ...

  8. Word公式装逼技巧,你绝对不会!

    Word论文排版是非常有技术含量的.只是纯文本格式时都有很多技巧,累倒一群人,更不用说还加上有数学公式了.有数学公式也就算了,问题是公式排版更是难上加难.想要在人前装逼一把?没有这些技巧你是绝对不行的 ...

  9. hadoop3.1.0 window win7 基础环境搭建

    https://blog.csdn.net/wsh596823919/article/details/80774805 hadoop3.1.0 window win7 基础环境搭建 前言:在windo ...

  10. php中一些常用的语句收集

    清空数据表   truncate 表名; http://blog.knowsky.com/234205.htm    常用的SQL语句实例 http://blog.csdn.net/vericlong ...