HDOJ 1159 Common Subsequence【DP】

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 44280 Accepted Submission(s): 20431

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

4

2

0

题意

求解两个字符串的最长公共子序列

思路

如果两个字符串的最后一个字符相等,那么由这最后一个字符组成的最长公共子序列就是 前面的最长公共子序列长度+ 1 然后往前推 就可以了

DP[i][j] = DP[i - 1][j - 1] + 1

如果不相等

DP[i][j] = max(DP[i - 1][j], DP[i][j - 1])

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std; typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
int dp[maxn][maxn]; int main()
{
string a, b;
while (cin >> a >> b)
{
int len_a = a.size(), len_b = b.size();
memset(dp, 0, sizeof(dp));
LL ans = 0;
for (int i = 0; i < len_a; i++)
{
if (b[0] == a[i])
{
dp[i][0] = 1;
ans = 1;
}
else if (i)
dp[i][0] = dp[i - 1][0];
}
for (int i = 0; i < len_b; i++)
{
if (a[0] == b[i])
{
dp[0][i] = 1;
ans = 1;
}
else if(i)
dp[0][i] = dp[0][i - 1];
}
for (int i = 1; i < len_a; i++)
{
for (int j = 1; j < len_b; j++)
{
if (a[i] == b[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
cout << dp[len_a - 1][len_b - 1] << endl;
}
}

HDOJ 1159 Common Subsequence【DP】的更多相关文章

  1. hdoj 1159 Common Subsequence【LCS】【DP】

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

  2. HDU 1159 Common Subsequence【dp+最长公共子序列】

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

  3. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  4. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  5. HDU 1159.Common Subsequence【动态规划DP】

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

  6. hdu 1159 Common Subsequence 【LCS 基础入门】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. HDOJ --- 1159 Common Subsequence

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

  8. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  9. HDU 1159 Common Subsequence (dp)

    题目链接 Problem Description A subsequence of a given sequence is the given sequence with some elements ...

随机推荐

  1. java程序调用kettle

    (1).将相应的kettle的jar包导入的java项目,主要的jar包有一下几个. (2).java程序. package cn.com.taiji.oosweb.test.web; import ...

  2. javaEE面试重点

    Hibernate工作原理及为什么要用? 原理: 1. 读取并解析配置文件 2. 读取并解析映射信息.创建SessionFactory 3. 打开Sesssion 4. 创建事务Transation ...

  3. Docker入门与应用系列(七)Docker图形界面管理之DockerUI

    1.dockeruiDockerrUI是一个基于Docker API提供图形化页面简单的容器管理系统,支持容器管理.镜像管理.1.1 下载镜像 docker pull abh1nav/dockerui ...

  4. NEU710(wanghang走迷宫)

    题目链接:传送门 题目大意:给你一个图,要从起点走到终点并且要吃够足够的金币才能出去,图上有金币(只能吃一次), 有传送门(用一次消耗1金币,必须有金币才能使用),问最少需要多少步才能出去.不能出去输 ...

  5. Linux下修改Mysql的用(root的密码及修改root登录权限

    修改的用户都以root为列. 一.知道原来的myql数据库的root密码: ①: 在终端命令行输入 mysqladmin -u root -p password "新密码" 回车  ...

  6. 高性能Web开发系列

    1. 高性能WEB开发基础 http://www.uml.org.cn/net/201404225.asp 2. 高性能WEB开发进阶(上) http://www.uml.org.cn/net/201 ...

  7. jsp 通用获取所有表单值传后台

    新建一个js文件,自定义一个jquery 函数. 在jsp页面引用 下面为:自定义函数 $.fn.GetDivJson = function (prifix,orgModel) { var $oute ...

  8. delphi ---break,exit,continue等跳出操作的区别

    1.break 强制退出最近的一层循环(注意:只能放在循环里:而且是只能跳出最近的一层循环),用于从for.while.repeat语句中强制退出 2.continue 用于从for.while.re ...

  9. delphi 遇到问题、报错等

    解决方法:using Windows

  10. Spoken English Practice( let me just pull over(pull,give))

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/6/26) ...