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. 「国家集训队」Crash的数字表格

    题目描述 求(对 \(20101009\) 取模,\(n,m\le10^7\) ) \[\sum_{i=1}^n\sum_{j=1}^m\operatorname{lcm}(i,j)\] 大体思路 推 ...

  2. 如何:使用 as 和 is 运算符安全地进行强制转换(C# 编程指南)

    如何:使用 as 和 is 运算符安全地进行强制转换(C# 编程指南) 由于对象是多态的,因此基类类型的变量可以保存派生类型.若要访问派生类型的方法,需要将值强制转换回该派生类型.不过,在这些情况下, ...

  3. 第1节 storm日志告警:1、 - 5、日志监控告警业务需求、代码、集群运行、总结

    如何解决短信或者邮件频繁发送的问题:每次发送的时候都先查询数据库记录,看一下有没有给这个人发送消息,上一次发送消息的时间是什么时候,如果发送时间间隔小于半个小时,就不要再发了 ============ ...

  4. 第1节 kafka消息队列:5、javaAPI操作

    8.kafka的API 详见代码   第一步:导入kafka的开发jar包 Kafka生产者 Kafka消费者

  5. maven更新JRE更改JSE1.5

    1. [代码]在maven的配置文件settings.xml中的<profiles>标签里添加如下代码,设置默认JRE编译版本为1.7 <profile> <id> ...

  6. Day2-K-Red and Black-HDU1312

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  7. 谈一下你对uWSGI和 nginx的理解(原理)

    要注意 WSGI / uwsgi / uWSGI 这三个概念的区分. WSGI是一种通信协议. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信. uWS ...

  8. 088、Java中String类之对象直接赋值

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  9. 071、Java面向对象之使用private封装属性

    01.代码如下: package TIANPAN; class Book { // 定义一个新的类 private String title; // 书的名字 private double price ...

  10. ROS常用库(五)navigation之Tutorials

    一.TF 详见古月居 https://www.guyuehome.com/355 重点:广播TF,订阅,编译时Cmakelist添加编译选项 broadcaster.sendTransform( tf ...