POJ 1458 最长公共子序列

题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致。

Sample Input :

abcfbc abfcab

programming contest

abcd mnp

Sample Output

4 2 0

分析:

输入两个串s1,s2, 设dp(i,j)表示: s1的左边i个字符形成的子串,与s2左边的j个 字符形成的子串的最长公共子序列的长度(i,j从0 开始算) dp(i,j) 就是本题的“状态”。

假定 len1 = strlen(s1),len2 = strlen(s2) 那么题目就是要求 MaxLen(len1,len2)

递推公式: if ( s1[i-1] == s2[j-1] ) dp(i,j) = dp(i-1,j-1) + 1; else dp(i,j) = Max(dp(i,j-1),dp(i-1,j) );

时间复杂度O(mn) m,n是两个字串长度

代码:

#include<iostream>
#include<cstring>
using namespace std;
#define N 1000
char str1[N];
char str2[N];
int dp[N][N];
int main() {
while(cin >> str1 >> str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
for(int i = ; i < len1; i++) dp[i][] = ;
for(int i = ; i < len2; i++) dp[][i] = ;
for(int i = ; i < len1; i++) {
for(int j = ; j < len2; j++) {
if(str1[i] == str2[j])
dp[i+][j+] = dp[i][j] + ;
else {
dp[i+][j+] = max(dp[i][j+], dp[i+][j]);
}
}
}
cout << dp[len1][len2] << endl;
}
return ;
}

POJ 1458 最长公共子序列(dp)的更多相关文章

  1. 【简单dp】poj 1458 最长公共子序列【O(n^2)】【模板】

    最长公共子序列可以用在下面的问题时:给你一个字符串,请问最少还需要添加多少个字符就可以让它编程一个回文串? 解法:ans=strlen(原串)-LCS(原串,反串); Sample Input abc ...

  2. POJ 1458 最长公共子序列

    子序列就是子序列中的元素是母序列的子集,且子序列中元素的相对顺序和母序列相同. 题目要求便是寻找两个字符串的最长公共子序列. dp[i][j]表示字符串s1左i个字符和s2左j个字符的公共子序列的最大 ...

  3. POJ 1458 最长公共子序列 LCS

    经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1 ...

  4. Common Subsequence POJ - 1458 最长公共子序列 线性DP

    #include <iostream> #include <algorithm> #include <string> #include <cstring> ...

  5. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  6. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  7. LCS最长公共子序列~dp学习~4

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...

  8. 【BZOJ2423】[HAOI2010]最长公共子序列 DP

    [BZOJ2423][HAOI2010]最长公共子序列 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

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

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

随机推荐

  1. MySQL数据库自动备份

    1.vi /home/wangcn/auto_log.sh#!/bin/bash #Shell Command For Backup MySQL Database Everyday Automatic ...

  2. np.zeros

    np.zeros构造一个全部由0组成的矩阵 用法:zeros(shape, dtype = float, order = 'C') 参数: shape:形状 dtype类型: t ,位域,如t4代表4 ...

  3. Linux 安装SSH

    ●centOS/redhat安装SSH 查询openssh server服务状态:systemctl status sshd 安装sshd命令: yum install openssh-server ...

  4. 简单实现Java的RMI——远程方法调用

    一.RMI简介: 说到RMI就不得不说RPC了. RPC:(Remote Procedure Call),远程过程调用. RMI(Remote Method Invocation),远程方法调用. R ...

  5. AtCoder Regular Contest 094 D Worst Case

    Worst Case 思路: 使 a <= b 当 a == b 时 或者 a == b - 1 时,答案显然为 2 * (a - 1) 否则找到最大的 c ,使得 c * c < a * ...

  6. Codeforces 931F - Teodor is not a liar!

    931F - Teodor is not a liar! 思路: 最长上升子序列 先差分数组染色 如果存在一个点,被所有区间包含,那么这张图一定是山峰状,如下图: 那么只要分别从前和从后找一个最长非严 ...

  7. CURL操作

    具体代码如下: <?php$curl=curl_init(); //初始化$url='http://www.ecshop.com';//curl_setopt(curl资源,选项标志,选项值)c ...

  8. ThinkPHP表单自动验证(注册功能)

    控制器中: 模型中: 视图中:

  9. Eclipse中打包插件Fat Jar的安装与使用

    转自:https://www.cnblogs.com/wbyp/p/6222182.html     Eclipse可以安装一个叫Fat Jar的插件,用这个插件打包非常方便,Fat Jar的功能非常 ...

  10. pythoncook 文件和io

    1.文件不存在,则写入:文件存在则,报错 try: with open('file','x') as f: f.write() except FileExistsError: print('file ...