Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41957   Accepted: 16927

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

这题三年之前估计就是看别人思路,自己写了一遍AC的,结果三年之后自己开始学习动态规划的时候,碰到了这种基础题目居然还是没想到思路,脸都不要了,真想敲自己脑袋啊。

题意很简单,求两个字符串的最长公共子序列。

标准的动态规划,用dp[i+1][j+1]表示到a的第i个字符,b的第j个字符时的最大长度。其实就是判断a[i]与b[j]是否相等,相等就在此基础之上加一。不相等就取dp[i][j+1]和dp[i+1][j]的最大值即可。

算法复杂度是O(i*j),i、j为两个字符串的长度。

代码:

#include<iostream>
#include<string>
#include<cstring>
using namespace std; int dp[500][500]; int main()
{
string a,b;
while(cin>>a>>b)
{
memset(dp,0,sizeof(dp));
int len1=a.length();
int len2=b.length(); int i,j; for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
{
if(a[i]==b[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
}
} cout<<dp[len1][len2]<<endl;
} return 0;
}

总结的话,就是自己为什么没能想到用dp[i][j]的数组来表示呢,我还咋想用dp[i]表示在第二个字符串中到第i个字符时的长度,然后之后在逐渐查找,但这样麻烦啊麻烦啊。所以总结就是自己为什么没能想到用这种方式表示呢?自己为什么没能想到用这种方式表示呢?

总而言之,只能记住这种最长公共子序列的方法,记住了这种方法,自己脑袋里的数据库也算多了一点,以后再遇到这类问题的时候还想不到的话,真的就要敲自己脑袋了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1458:Common Subsequence的更多相关文章

  1. 【POJ - 1458】Common Subsequence(动态规划)

    Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some e ...

  2. UVa 10405 & POJ 1458 Longest Common Subsequence

    求最长公共子序列LCS,用动态规划求解. UVa的字符串可能含有空格,开始用scanf("%s", s);就WA了一次...那就用gets吧,怪不得要一行放一个字符串呢. (本来想 ...

  3. HDU1159 && POJ1458:Common Subsequence(LCS)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  4. 算法:Common Subsequence(动态规划 Java 最长子序列)

    Description A subsequence of a given sequence is the given sequence with some elements (possible non ...

  5. HDU 1159:Common Subsequence(LCS模板)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. hdu-题目1159:Common Subsequence

    http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Oth ...

  7. OpenJudge/Poj 1458 Common Subsequence

    1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...

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

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

  9. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

随机推荐

  1. while语句及批量创建用户!

    1.while 循环语句的作用:重复测试某个条件,只要条件成立则反复执行2.while 语句结构while 条件测试操作do命令序列done ============================= ...

  2. Linux CentOS7 rsync通过服务同步、linux系统日志、screen工具

    一.rsync通过服务同步 rsyncd.conf配置文件详解 port:指定在哪个端口启动rsyncd服务,默认是873端口. log file:指定日志文件. pid file:指定pid文件,这 ...

  3. Linux 下安装 FFmpeg

    1. 下载源代码: git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg 2. 编译 ./configure --enable-shared --pre ...

  4. 1 网页及浏览器内核&Web标准

    网页的组成: 主要由文字.图像和超链接等元素构成,还可以包含音频.视频以及flash. 浏览器内核: 浏览器内核分为两部分: 1 渲染引擎(layout engineer) 渲染引擎负责取得网页的内容 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:显示下拉式功能

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

  6. jgrid异步数据加载

    参考:https://blog.csdn.net/hurryjiang/article/details/7077725

  7. 用于云计算的自我更新、自我修补的Linux版本已发布!

    自动化是 IT 行业的增长趋势,其目的是消除重复任务中的手动干扰.Oracle 通过推出 Oracle Autonomous Linux 向自动化世界迈出了又一步,这无疑将使 IoT 和云计算行业受益 ...

  8. luogu P2766 最长不下降子序列问题

    第一问可以直接DP来做,联想上一题,线性规划都可以化为网络流?我们可以借助第一问的DP数组,来建立第二问第三问的网络流图,考虑每一种可能,都是dp数组中满足num[i]>=num[j]& ...

  9. 「SP1716」GSS3 - Can you answer these queries III

    传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...

  10. MyBatis学习之简单增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多

    一.用到的实体类如下: Student.java package com.company.entity; import java.io.Serializable; import java.util.D ...