题目链接
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. DT6.0关于SQL注入漏洞修复问题

    阿里云安全平台提示:Destoon SQL注入,关于: Destoon的/mobile/guestbook.php中$do->add($post);这行代码对参数$post未进行正确转义,导致黑 ...

  2. SignalR入门二、使用 SignalR 2 实现服务器广播

    一.概述 这篇教程通过实现一个股票报价的小程序来讲解如何使用SignalR进行服务器端的推送,服务器会模拟股票价格的波动,并把最新的股票价格推送给所有连接的客户端,最终的运行效果如下图所示. 教程:使 ...

  3. 写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”;一个容易被忽略的bug

    代码如下: public class test{ public static void main(String args[]){ String test=test("sahsjkshabsh ...

  4. 洛谷 P3376 【模板】网络最大流 题解

    今天学了网络最大流,EK 和 Dinic 主要就是运用搜索求增广路,Dinic 相当于 EK 的优化,先用bfs求每个点的层数,再用dfs寻找并更新那条路径上的值. EK 算法 #include< ...

  5. Xamarin开发及学习资源

    入行文章指引 移动开发下Xamarin VS PhoneGap 跨平台开发 许多企业希望能够通过开发移动应用程序,来提升企业业务水平,开发原生App时往往又缺少专业的Objective C 或 Jav ...

  6. Lightning Web Components 组件样式(四)

    要将样式与组件进行绑定,需要创建一个同名的样式文件,这样样式将会自动应用到组件 在组件中定义的样式的作用域是属于组件的,这样允许组件可以在不同的上下文中可以复用, 可以阻止其他组件的样式的复写 css ...

  7. Truffle - 以太坊Solidity编程语言开发框架

    http://truffle.tryblockchain.org/ Truffle框架 Truffle是什么? Truffle是针对基于以太坊的Solidity语言的一套开发框架. 本身基于JavaS ...

  8. CSS链接伪类:超链接的状态

    一.状态: a:link{属性:值;} 链接默认状态 a:visited{属性:值;} 链接访问之后的状态 a:hover{属性:值;} 鼠标放到链接上显示的状态 a:active{属性:值;} 链接 ...

  9. hhhhh臭不要脸//捂脸)多不好意思啊you进步惹

    如题↑↑↑ 千万不要相信题目 还是看图说话吧↓↓↓ 我真的蒟蒻啊,,,准确率在70边缘徘徊,卑微☹ 不过还是侥幸地进入了前 30 名! 今天七夕欸,然鹅,,, qq空间里面弥漫着恋爱的酸臭味 香气,‘ ...

  10. TRUNCATE、Drop、Delete 的用法

    //Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用. truncate table1; drop table1; dele ...