Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55301    Accepted Submission(s): 25537

Problem 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.
The program input is from a text file. Each
data set in the file contains two strings representing the given
sequences. The sequences are separated by any number of white spaces.
The input data are correct. 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


题目大意与分析

就是求最长公共子序列,动态规划即可。

dp[i][j]代表A序列前i-1个元素与B序列前j-1个元素的最长公共子序列的个数,两层循环,相等就++,否则取当前最大的

代码

#include<bits/stdc++.h>

using namespace std;

string x,y;
int i,j,dp[][]; int main()
{
while(cin>>x>>y)
{
memset(dp,,sizeof(dp));
for(i=;i<x.size();i++)
{
for(j=;j<y.size();j++)
{
if(x[i]==y[j])
dp[i+][j+]=dp[i][j]+;
else
dp[i+][j+]=max(dp[i+][j],dp[i][j+]);
}
}
cout<<dp[x.size()][y.size()]<<endl;
}
}

HDU 1159 Common Subsequence (动态规划、最长公共子序列)的更多相关文章

  1. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  2. hdu 1159 Common Subsequence(LCS最长公共子序列)

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

  3. hdu 1159 Common Subsequence (最长公共子序列 +代码)

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

  4. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  5. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

  6. hdu 1159 Common Subsequence(最长公共子序列,DP)

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

  7. HDU - 1159 Common Subsequence (最长公共子序列)

    A subsequence of a given sequence is the given sequence with some elements (possible none) left out. ...

  8. 杭电1159 Common Subsequence【最长公共子序列】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 解题思路:任意先给出两个字符串 abcfbc abfcab,用dp[i][j]来记录当前最长的子 ...

  9. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  10. POJ 1458 Common Subsequence 【最长公共子序列】

    解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序 ...

随机推荐

  1. Phaserjs怎样用ES6开发游戏

    想用ES6语法开发phaserjs游戏,是phaserCE,但是不知道怎么导入,总是报错,后来经过多次尝试,解决方法如下: 干脆不导入,直接暴露到window里,然后模块化的代码全部在window.o ...

  2. vue启动问题(You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.)

    解决vue启动出现: 在build/webpack.base.conf.js文件中,把...(config.dev.useEslint ? [createLintingRule()] : [])注释或 ...

  3. i3wm

    1.音量调节(alsa-utils) alsamixer: alsamixer is a graphical mixer program for the Advanced Linux Sound Ar ...

  4. 在echart组件下用canvans画三角形

    //使用的canvans绘制的三角形 drawArrow(){ var canvas = document.createElement('canvas');//创建一个元素 canvas.width ...

  5. JavaScript 数组2—关联数组

    ㈠什么是关联数组 可以自定义下标名称的数组 ㈡为什么 索引数组中的数字下标没有明确的意义 ㈢何时 只希望每个元素都有专门的名称时 ㈣如何:2步 1)创建空数组 2)向空数组中添加新元素,并自定义下标名 ...

  6. BZOJ 5084: hashit 后缀自动机(原理题)

    比较考验对后缀自动机构建过程的理解. 之前看题解写的都是树链的并,但是想了想好像可以直接撤销,复杂度是线性的. 自己想出来的,感觉后缀自动机的题应该不太能难倒我~ 注意:一定要手画一下后缀自动机的构建 ...

  7. Gene co-expression analysis for functional classification and gene–disease predictions

  8. 论文阅读:Offloading Distributed Applications onto SmartNICs using iPipe

    摘要: 包含丰富计算资源的新兴多核SoC SmartNIC具有卸载通用数据中心服务器任务的潜力,但是目前尚不清楚如何有效地使用SmartNIC并最大程度地减少卸载收益,尤其是对于分布式应用程序. 为此 ...

  9. vue-cli中route和router的区别

    在使用vue-router的时候经常分不清router和route的区别: 在函数式编程中: this.$router.push('/login') 或者Router.push() 在动态获取路由参数 ...

  10. python学习之路(7)

    调用函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: http: ...