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. Mac安装navigate premium

    转自Navicat Premium for Mac v12.0.22.0 破解教程,macOS上手动破解,无需补丁,无毒今天换了电脑,想装一个Navicat,突然发现了这篇破解教程,竟爱不释手,顾Co ...

  2. [经验] 关于 Java 中的非空判断

    在写项目的时候, 遇到一个问题 假设有一个控制层接口为: @ResponseBody @RequestMapping(value = "test", method = Reques ...

  3. js实现超范围的数相加

    在js中能表示的最大安全整数是 9007199254740991,可以用API Number.MAX_SAFE_INTEGER 看一下  超出范围就会发生精度丢失,像这样 解决方法: 相当于一个字符串 ...

  4. (转)jquery.validate插件的使用

    JQuery Validate使用总结:一.导入js库<script src="../js/jquery.js" type="text/javascript&quo ...

  5. idea增删改查

    idea应用mybatis写增删改查 entity层 private Integer id;private String userCode;private String userName;privat ...

  6. 新闻网大数据实时分析可视化系统项目——7、Kafka分布式集群部署

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  7. JS监听video视频播放时间

    采用原生时间监听element.addEventListener(event, function, useCapture) //监听播放时间 var video = document.getEleme ...

  8. 如何确定Redis集群中各个节点的主从关系

    1.首先通过命令(以192.168.203.141为例,-c代表集群的意思) ./redis-cli -h 192.168.203.141 -p 8001 -c 2.然后在输入  cluster no ...

  9. 解决Eclipse Debug 断点调试的source not found问题

    写完代码进行调试的时候,经常会用到断点调试,一步步检测问题,但有时候eclipse有时候无法进入断点,这样就失去了断点的意义,原因是debug无法找到该项目的源代码,解决方法如下 1,打开debug ...

  10. python绘制WordCloud词云图

    目录 前言 核心代码 测试 前言 当我们想快速了解书籍.小说.电影剧本中的内容时,可以绘制 WordCloud 词云图,显示主要的关键词(高频词),可以非常直观地看到结果 核心代码 from word ...