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. 高版本的Hibernate

    我的查询语句是“from TA pojo where pojo.tbs.name='tb1'”,可结果报错. 高版本的Hibernate不能这样查Set了, 要改成这样: from TA pojo i ...

  2. [ 剑指Offer ] Week2 学习笔记

    打印从1到最大的n位数 题解:由于未知n的大小,需要考虑大数问题.在这样的情况下,逐位地将字符串转换为数字输出,不会有溢出的可能.使用全排列的方式列出所有数字,省去了需要考虑进位的可能. 初始化数组, ...

  3. 小程序中data数据的处理方法总结

    wxml代码: <view class="container"> <view wx:for="{{list}}" wx:key="t ...

  4. postman提交文件

    说明 1.Headers中添加 Content-Type      multipart/form-data 2.Body 中选择form-data 并添加 需要传的参数名和值 最后新的一行选择file ...

  5. centos安装出现dracut-initqueue timeout错误

    dracut:/# cd /dev dracut:/# ls 这样子你就会看到所有的设备信息. 找到sda4,  不一定是sda4,有可能是其他的,实在不确定就拔了优盘看一下哪个消失了 dracut: ...

  6. 使用element-ui组件el-table时需要修改某一行或列的样式(包含解决选择器无效问题)

    在后台管理系统项目中,经常会使用element-ui中的组件el-table(表格)来展示列表数据. 当展示数据的时候,可能就需要给给某一行或者列设置特殊的样式,在查询文档是我遇到了一些问题:包括设置 ...

  7. 新闻网大数据实时分析可视化系统项目——2、linux环境准备与设置

    1.Linux系统常规设置 1)设置ip地址 使用界面修改ip比较方便,如果Linux没有安装操作界面,需要使用命令:vi /etc/sysconfig/network-scripts/ifcfg-e ...

  8. 第1节 storm编程:4、storm环境安装以及storm编程模型介绍

    dataSource:数据源,生产数据的东西 spout:接收数据源过来的数据,然后将数据往下游发送 bolt:数据的处理逻辑单元.可以有很多个,基本上每个bolt都处理一部分工作,然后将数据继续往下 ...

  9. STM32CubeIDE printf 串口重定向

  10. Oracle笔记--PL/SQL(Procedure Language & Structured Query Language)

    1.PL/SQL是一种高级数据库程序设计语言,专门用于在各种环境下对Oracle数据库进行访问.该语言集成于数据库服务器中,所以PL/SQL代码可以对数据进行快速高效的处理. 2.PL/SQL是对SQ ...