Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45763   Accepted: 18737

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         abfcab
programming contest
abcd mnp

Sample Output

4
2
0
题目大意:给你两个字符串,求两个字符串最长公共子串的长度,结合题意应该很容易理解题意。
思路分析:啥也不说,经典DP,首先确定状态以及状态的储存方法,用一个二维数组f[n][n]来进行储存,f[i][j]表示第一个字符串的前i位与后一个字符串的前j位最长公共
子序列长度,然后需要思考状态如何进行转移,若s1[i]==s2[j],f[i][j]=f[i-1][j-1]+1;若s1[i]!=s2[j],则f[i][j]应该与f[i][j-1]或者f[i-1][j]一致,具体
看那一个大,即f[i][j]=max{f[i][j-1],f[i-1][j]},这样状态转移方程也确定了。在代码实现的时候需要注意几点,首先字符串长度并没有给你,开到210就可以了,另外
初始状态要合理进行处理,否则会出现数组越界的错误!
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int maxn=210;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
int main()
{
    int i,j;
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        int l1=strlen(s1);
        int l2=strlen(s2);
        for(i=0;i<l1;i++)
        {
            for(j=0;j<l2;j++)
            {
                if(s1[i]==s2[j])
                {
                    if(i>=1&&j>=1) dp[i][j]=dp[i-1][j-1]+1;
                    else dp[i][j]=1;
                }
                else
                {
                    if(i>=1&&j>=1) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                    else if(j>=1) dp[i][j]=dp[i][j-1];
                    else if(i>=1) dp[i][j]=dp[i-1][j];
                    else dp[i][j]=0;
               }
            }
        }
        int t=dp[0][0];
        for(i=0;i<l1;i++)
            for(j=0;j<l2;j++)
          if(t<dp[i][j]) t=dp[i][j];
        cout<<t<<endl;
    }
    return 0;
}
—

poj1458 求最长公共子序列 经典DP的更多相关文章

  1. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  2. Java实现 LeetCode 583 两个字符串的删除操作(求最长公共子序列问题)

    583. 两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例: 输入: " ...

  3. [algorithm]求最长公共子序列问题

    最直白方法:时间复杂度是O(n3), 空间复杂度是常数 reference:http://blog.csdn.net/monkeyandy/article/details/7957263 /** ** ...

  4. HDU 1243 反恐训练营 (动态规划求最长公共子序列)

    反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. 【dp】求最长公共子序列

    [题目描述] 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X=<x1,x2,…,xm>X=<x1,x2,…,xm>,则另一序列Z=<z1 ...

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

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

  7. P1439 【模板】最长公共子序列(DP)

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

  8. POJ-1458(LCS:最长公共子序列模板题)

    Common Subsequence POJ-1458 //最长公共子序列问题 #include<iostream> #include<algorithm> #include& ...

  9. 洛谷 P1439 【模板】最长公共子序列(DP,LIS?)

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

随机推荐

  1. struts2 MessageStoreInterceptor 拦截器的使用

    MessageStoreInterceptor 拦截器可以把和该 Action 相关的 messages, errors 和 field errors(下称 "消息") 保存到 s ...

  2. python安装——Windows平台

    刚刚申请博客,第一次写随笔,记录下自己最近的学习情况,希望自己能够不断的学习,不断的丰富自己~ 最近刚开始学python,记录一下,希望大家相互学习,批评指正~ 1.下载python:https:// ...

  3. R教程计划

    提起数据挖掘,似乎会有很多人望而却步,从生产规划到到规律分析,从生物医学到航天科技,到处都有数据挖掘工程师留下的影子. 通过对比SAS,SPSS,以及R,最终选定了R, 不为什么,免费且高效才是硬道理 ...

  4. 完美解决ListView 与 ScrollView 共存问题

    1:首先设置ListView的高度,在setAdapter之后调用此方法. public static void setListViewHeightBasedOnChildren(ListView l ...

  5. XJOI网上同步训练DAY2 T1

    [问题描述] 为了迎接校庆月亮中学操场开始施工.不久后操场下发现了很多古墓这些古墓中有很多宝藏.然而学生们逐渐发现自从操场施工之后学校的运气就开始变得特别不好.后来经过调查发现古墓下有一个太守坟由于操 ...

  6. C51工具是怎么进行覆盖分析的

    C51工具针对8051微控制器的有限存储器资源进行了优化设计. 为了最有效地利用存储器,根据一个很容易解释的方法,自动变量和函数参数在存储器中均进行覆盖处理. 首先,连接器根据源程序生成调用树.例如: ...

  7. hidden symbol ... is referenced by DSO

    在Linux上编译Qt的时候configure出来的Makefile传递给g++的参数visiblility=hidden,然后就会调用Qt库所使用的第三方库libpng库源代码函数声明添加上__at ...

  8. cf486B OR in Matrix

    B. OR in Matrix time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  9. Hdu5126-stars(两次CDQ分治)

    题意: 简化就是有两种操作,一种是插入(x,y,z)这个坐标,第二种是查询(x1,y1,z1)到(x2,y2,z2)(x1<=x2,y1<=y2,z1<=z2)的长方体包含多少个点. ...

  10. 减少GC开销的5个编码技巧

    在这篇文章中,我们来了解一下让代码变得高效的五种技巧,这些技巧可以使我们的垃圾收集器(GC)在分配内存以及释放内存上面,占用更少的CPU时间,减少GC的开销.当内存被回收的时候,GC处理很长时间经常会 ...