子序列就是子序列中的元素是母序列的子集,且子序列中元素的相对顺序和母序列相同。

题目要求便是寻找两个字符串的最长公共子序列。

dp[i][j]表示字符串s1左i个字符和s2左j个字符的公共子序列的最大长度。

注意s1第i个字符为s1[i-1]

于是有递推公式:

对于abcfbc和abfcab两个字符串,求公共子串的最大长度的过程如图:

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
char s1[maxn], s2[maxn];
int dp[maxn][maxn]; int main(void)
{
#ifdef LOCAL
freopen("1458in.txt", "r", stdin);
#endif while(cin >> s1 >> s2)
{
int Lenth1 = strlen(s1);
int Lenth2 = strlen(s2);
memset(dp, , sizeof(dp)); int i, j;
for(i = ; i <= Lenth1; ++i)
for(j = ; j <= Lenth2; ++j)
{
if(s1[i-] == s2[j-])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i-][j], dp[i][j-]);
} printf("%d\n", dp[Lenth1][Lenth2]);
}
return ;
}

代码君

POJ 1458 最长公共子序列的更多相关文章

  1. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  2. POJ 1458 最长公共子序列 LCS

    经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1 ...

  3. 【简单dp】poj 1458 最长公共子序列【O(n^2)】【模板】

    最长公共子序列可以用在下面的问题时:给你一个字符串,请问最少还需要添加多少个字符就可以让它编程一个回文串? 解法:ans=strlen(原串)-LCS(原串,反串); Sample Input abc ...

  4. Common Subsequence POJ - 1458 最长公共子序列 线性DP

    #include <iostream> #include <algorithm> #include <string> #include <cstring> ...

  5. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  6. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  7. poj 1952 最长公共子序列计数

    看代码就懂了  不解释  3 1 1 1 1 2 2 2 1 1 1 3  第一个3 和最后一个 3 只需要一个就够了,,, #include<iostream> #include< ...

  8. Human Gene Functions POJ 1080 最长公共子序列变形

    Description It is well known that a human gene can be considered as a sequence, consisting of four n ...

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

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

随机推荐

  1. ubuntu修改ip、网关、dns等

    一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用vi编辑器(也可以 ...

  2. Activity学习(四)——简单切换

    理论学习Activity之后,我们就来具体的实战,Activity之间相互切换依靠的是“ 意图 ”(Intent),这个 Intent 包含了要跳转到的Activity的一些信息,因为Activity ...

  3. POJ 1523 SPF(求割点)

    题目链接 题意 : 找出图中所有的割点,然后输出删掉他们之后还剩多少个连通分量. 思路 : v与u邻接,要么v是u的孩子,要么u是v的祖先,(u,v)构成一条回边. //poj1523 #includ ...

  4. Codeforces Round #260 (Div. 2) A~C

    题目链接 A. Laptops time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputo ...

  5. Linux网络编程3——socket

    宏定义 首先介绍两个宏定义,看如下代码 代码1 /************************************************************************* & ...

  6. 浅说Java中的反射机制(一)

    在学习传智播客李勇老师的JDBC系列时,会出现反射的概念,由于又是第一次见,不免感到陌生.所以再次在博客园找到一篇文章,先记录如下: 引用自java中的反射机制,作者bingoideas.(()为我手 ...

  7. hdu 4159 Indomie (DP,数学概率)

    推出数学公式: #include<stdio.h> #include<string.h> __int64 C(int m,int n) { __int64 tmp=; if(m ...

  8. hdu 1515 Anagrams by Stack

    题解: 第一:两个字符不相等(即栈顶字符与目标字符不相等):这种情况很容易处理,将匹配word的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...

  9. Linux inotify功能及实现原理

    http://www.cnblogs.com/jiejnan/archive/2012/05/18/2507476.html 简介: 当需要对 Linux®文件系统进行高效率.细粒度.异步地监控时,可 ...

  10. Hibernate笔记——缓存机制详细分析

    原文:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html ========================================== ...