Common Subsequence

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

这道题就是求两个字符串的最长公共子序列的长度,如图:


求最长公共子序列长度,可以分阶段,先找第一串第一个字符与第二串的长度,
再找第一串前两个字符与第二串的最长公共子序列长度,
以此类推。。。。

上图是abcfb 与 abfcab,求这两串的最长公共子序列
图中设置的是二维数组,格内的数为行列号,比如,第一个00表示第0行第0列,
由图可以很清晰的看出表达式:
设 abcfb为str1[6],abfcab为str2[7],i为行号,j为列号。
当str1[i]==str2[j]时,长度为 二维数组[i-1][j-1]+1,
不相等时为上格子与前一格子最大值(即max(二维数组[i-1][j],二维数组[i][j-1]);
这样算下来,最右下角的必定为最长公共子序列的长度。

代码如下:

#include <iostream>
#include <string>
using namespace std; int arr[1001][1001]; int main()
{
    int i,j,s1,s2;
    char str1[1001],str2[1001];
    
    while(cin>>str1>>str2)
    {
        s1=strlen(str1);
        s2=strlen(str2);
        
        for(i=1;i<=s1;++i)            // 让刚开始i=1,j=1可以避免判断边界值的时候,使代码简练
            for(j=1;j<=s2;++j)
            {
                
                if(str1[i-1]==str2[j-1])
                    arr[i][j]=arr[i-1][j-1]+1;
                else
                    arr[i][j]=(arr[i-1][j]>arr[i][j-1])?arr[i-1][j]:arr[i][j-1];
            }         cout<<arr[s1][s2]<<endl;
    }
    return 0;
}

ACMDP之最长公共子序列长度—HDU1159的更多相关文章

  1. codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)

    题目描述 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y ...

  2. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

  3. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  4. 准备NOIP2017 最长公共子序列(模版)

    一些概念: (1)子序列: 一个序列A = a1,a2,--an,中任意删除若干项,剩余的序列叫做A的一个子序列.也可以认为是从序列A按原顺序保留任意若干项得到的序列.例如:   对序列 1,3,5, ...

  5. 动态规划 - 最长公共子序列(LCS)

    最长公共子序列也是动态规划中的一个经典问题. 有两个字符串 S1 和 S2,求一个最长公共子串,即求字符串 S3,它同时为 S1 和 S2 的子串,且要求它的长度最长,并确定这个长度.这个问题被我们称 ...

  6. uva111动态规划之最长公共子序列

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=74662#problem/C     A B C D E C - Largest Rect ...

  7. [BZOJ2423][HAOI2010]最长公共子序列

    [BZOJ2423][HAOI2010]最长公共子序列 试题描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x ...

  8. lintcode:最长公共子序列

    题目 最长公共子序列 给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度. 样例 给出"ABCD" 和 "EDCA",这个LCS是 "A& ...

  9. 【LCS,LIS】最长公共子序列、单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

随机推荐

  1. Java RMI(远程方法调用)开发

    参考 https://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch2.html http://www.cnblogs.com/wxi ...

  2. 如何使用KMS激活win10和office

    首先你需要下载一个kms软件,地址: https://yunpan.cn/cRxVNy2LRXjBt (提取码:d5d8) 然后搭建kms服务器,很简单.启动软件,选择“附加”Tab, 点连接到服务器 ...

  3. [转]CentOS Yum 命令详解

    总所周知,Redhat和Fedora的软件安装命令是rpm,但是用rpm安 装软件最大的麻烦就是需要手动寻找安装该软件所需要的一系列依赖关系,超级麻烦不说,要是软件不用了需要卸载的话由于卸载掉了某个依 ...

  4. Centos开启FTP及用户配置

    vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

  5. Painting The Wall 期望DP Codeforces 398_B

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. HTML5 Geolocation

    http://diveintohtml5.info/geolocation.html http://msdn.microsoft.com/en-us/library/windows/apps/hh44 ...

  7. SessionId

    http://www.codeweblog.com/session-cookie-jsessionid-url-rewriting/

  8. 【BZOJ 1594】 [Usaco2008 Jan]猜数游戏 (二分+并查集)

    1594: [Usaco2008 Jan]猜数游戏 Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在牛棚后面 ...

  9. JavaScript String支持的辅助format函数+【分页1】

    /** ) {         && ; i < arguments.length; i++) {                 : int.Parse(Request.Par ...

  10. MySQL show status详解

    http://www.sandzhang.com/blog/2010/04/07/mysql-show-status-explained-detail/ 要查看MySQL运行状态,要优化MySQL运行 ...