题目链接
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: Accepted:

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


中文题目:

给出两个字符串,求出这样一个最长的公共子序列的长度——子序列的每个字符都能在两个原串中找到,且每个字符的先后顺序和原串中的先后顺序一致。

解题思路:

步骤1-找子问题:将原问题可以分解为求s1左边i个字符的子串和s2左边j个字符子串的最长公共子序列。

步骤2-确定状态:MaxLen(i,j)表示上述最长公共子序列的长度,即为本题的状态。

步骤3-确定状态转移方程:

  • MaxLen(n,0)=0, MaxLen(0,m)=0 (n=0,1,2...len1, m=1,2...len2)
  • if(s1[i-1]==s2[j-1]) MaxLen(i,j) = MaxLen(i-1,j-1)+1;
  • else MaxLen(i,j) = Max(MaxLen(i,j-1), ManLen(i-1,j));

重点在于状态转移方程的书写,这一题讲义PPT中画的图很好,言简意赅,我一开始想的是计算s2中以xk为终点的字串在s1中的公共子序列,但是发现自己对题意的理解有误,子串中的各字母是可以隔开的,因此逐个字符比较是最好的。既然是逐个字符相比较,那么自然也要考虑s1的位置。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; char s1[];
char s2[];
int maxLen[][]; int main()
{
while (cin >> s1 >> s2)
{
int length1 = strlen(s1);
int length2 = strlen(s2);
for (int i = ; i <= length1; i++)
maxLen[i][] = ;
for (int j = ; j <= length2; j++)
maxLen[][j] = ;
for (int i = ; i <= length1; i++)
{
for (int j = ; j <= length2; j++)
{
if (s1[i - ] == s2[j - ])
maxLen[i][j] = maxLen[i - ][j - ] + ;
else
maxLen[i][j] = max(maxLen[i - ][j], maxLen[i][j - ]);
}
}
cout << maxLen[length1][length2] << endl;
}
return ;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N = ;
char s1[N], s2[N];
int l1, l2;
int dp[N][N]; int DP()
{
memset(dp, , sizeof(dp));
for (int i = ; i <= l1; i++)
{
for (int j = ; j <= l2; j++)
{
if (s1[i-] == s2[j-])dp[i][j] = dp[i - ][j - ] + ;
else dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
}
}
return dp[l1][l2];
} int main()
{
while (scanf("%s%s", s1, s2) != EOF)
{
l1 = strlen(s1);
l2 = strlen(s2);
printf("%d\n", DP());
}
//system("pause");
return ;
}

二刷

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. Linux CentOS7.x安装docker全过程

    1.在安装docker之前,首先使用yum -y remove docker命令移除系统中已有的旧版本的docker yum -y remove docker 这里显示该系统没有安装过docker: ...

  2. 【http】Coolie 属性

    expires属性 指 定了coolie的生存期,默认情况下coolie是暂时存在的,他们存储的值只在浏览器会话期间存在,当用户推出浏览器后这些值也会丢失,如果想让 cookie存在一段时间,就要为e ...

  3. Java 15周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  4. CollectionUtils.select用法

    import java.util.ArrayList;import java.util.List; import org.apache.commons.collections.CollectionUt ...

  5. RaiseFailFastException函数

    引发绕过所有异常处理程序(基于帧或矢量)的异常.引发此异常将终止应用程序并调用Windows错误报告(如果Windows错误报告正在运行). 原型: VOID WINAPI RaiseFailFast ...

  6. JS变量和变量交换的三种方法

    一.what 变量就是用来存储数据的容器 二.how 通过var 关键字定义一个变量 var n1; //定义变量 变量的赋值:通过赋值运算符“=” 给变量赋值. var n2=123; //定义变量 ...

  7. 洛谷 P2947 [USACO09MAR]向右看齐Look Up

    目录 题目 思路 \(Code\) 题目 戳 思路 单调栈裸题 \(Code\) #include<stack> #include<cstdio> #include<st ...

  8. js中forEach,for in,for of循环的用法详解

    一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...

  9. 读入优化&输出优化

    读入优化 int read() { ; ') ; '; ') num=num*+c-'; return ff*num; } 输出优化 void write(int x) { ) { putchar(' ...

  10. 前端base64加密

    一.Base64编码表 码值 字符 码值 字符 码值 字符 码值 字符 0 A 16 Q 32 g 48 w 1 B 17 R 33 h 49 x 2 C 18 S 34 i 50 y 3 D 19 ...