解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序列的长度

a    b    f    c    a    b

0    0    0    0    0   0    0

a  0    1     1    1    1   1    1

b  0    1     2    2    2   2    2

c  0    1     2    2    3   3    3

f  0     1     2    3    3   3   3

b 0     1    2     3   3    3   4

c  0     1    2     3   4   4    4

其中 s1 abcfbc

s2 abfcab

以s1中的a来分析,用它与s2中的字母比较,如果相同,那么dp[i][j]=dp[i-1][j-1]+1(遇到相同的字母公共子序列的长度增加1)

如果不同的话,dp[i][j]=max(dp[i-1][j],dp[i][j-1])                 (即取相邻两种情况中的最大值)

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39737   Accepted: 15977

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
#include<stdio.h>
#include<string.h>
#define maxn 10010
int dp[maxn][maxn]; int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
char s1[maxn],s2[maxn];
int len1,len2,i,j;
while(scanf("%s %s",&s1,&s2)!=EOF)
{
len1=strlen(s1);
len2=strlen(s2); for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
if(s1[i-1]==s2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
printf("%d\n",dp[len1][len2]);
} }

  

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

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

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  2. POJ 1458 Common Subsequence 最长公共子序列

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  3. POJ 1458 Common Subsequence 最长公共子序列 LCS

    LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...

  4. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)

    题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...

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

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

  6. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  7. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

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

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

  9. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  10. LCS修改版(Longest Common Subsequence 最长公共子序列)

    题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...

随机推荐

  1. "国学"能带给孩子什么?

       您是家长吗?   是!就点标题下方蓝色的 静心坊 三字,然后点关注!您就可以享受到我们为您提供的最新教育信息. 读国学,除大量识字之外,还能带给孩子什么呢? 男孩子读国学,国学能教会他做一个正直 ...

  2. JS 100元购物卡,牙刷5元,香皂2元、洗发水15元 100元正好花完有多少种可能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Vue2实例中的data属性三种写法与作用

    <script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app ...

  4. tree -l命令参考

    一.简介 Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing( ...

  5. Codeforces Round #447 (Div. 2)E. Ralph and Mushrooms

    Ralph is going to collect mushrooms in the Mushroom Forest. There are m directed paths connecting n  ...

  6. Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )

    题意: 三角形数.五边形数和六角形数分别由以下公式给出:       三角形数 Tn=n(n+1)/2 1, 3, 6, 10, 15, - 五边形数 Pn=n(3n−1)/2 1, 5, 12, 2 ...

  7. Linux 查看用户命令

    1.Linux里查看所有用户 (1)在终端里.其实只需要查看 /etc/passwd文件就行了. (2)看第三个参数:500以上的,就是后面建的用户了.其它则为系统的用户. 或者用cat /etc/p ...

  8. 模仿学习小游戏外星人入侵-Python学习,体会“函数”编程

    游戏类如下: # !/usr/bin/python # -*- coding:utf-8 -*- """ Author :ZFH File :alien.py Softw ...

  9. POJ 1948

    这道题我记得是携程比赛上的一道. 开始时想直接设面积,但发现不可以,改设能否构成三角形.设dp[i][j][k]为前i根木棍构成边长为j和k的三角形,那么转移可以为dp[i][j][k]=dp[i-1 ...

  10. 两列等高布局 padding+margin的负值 CSS布局奇淫技巧之-多列等高

    代码: 效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...