LCS 线性DP入门

C - Common Subsequence

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.

Input

abcfbc abfcab
programming contest
abcd mnp

Output

4
2
0

Sample Input

abcfbc abfcab
programming contest
abcd mnp

Sample Output

4
2
0题意:最长上升子序列思路:基础线性DP状态:DP【i】【j】表示截至字符串s1前i个元素,字符串s2前j个元素最长上升子序列目标:MAX(DP【i】【j】)状态转移:if(s1[i]==s2[k])     dp[i][k]=max(dp[i][k],dp[i-1][k-1])+1;               else                      dp[i][k]=max(dp[i-1][k],dp[i][k-1]);决策:s1【i】与s2【j】是否相等代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#define ll long long int
using namespace std;
int dp[1005][1005];
char s1[1005],s2[1005];
int main()
{
    while(scanf("%s%s",s1+1,s2+1)!=EOF)
    {
        memset(dp,0,sizeof(dp));//初始化
        int ss1=strlen(s1+1),ss2=strlen(s2+1);
        int MAX=-1;
        for(int i=1;i<=ss1;i++)
           for(int k=1;k<=ss2;k++)
           {
                   if(s1[i]==s2[k])
                      dp[i][k]=max(dp[i][k],dp[i-1][k-1])+1;
                   else
                      dp[i][k]=max(dp[i-1][k],dp[i][k-1]);
                   MAX=max(MAX,dp[i][k]);
           }
        cout<<MAX<<endl;
       }

    return 0;
}
 

原文地址:https://www.cnblogs.com/1911087165zzx/p/11348146.html

时间: 2024-10-09 23:50:24

LCS 线性DP入门的相关文章

LA 4256 Salesmen 线性dp

// LA 4256 Salesmen 线性dp // // 像LCS和LIS问题类似,因为每次修改一个值,都是根据 // 前一个值决定的,那么最后一个结尾的数字肯定要作为 // 状态,而长度作为状态是一目了然的 // // d[i][j]表示长度为i,最后以j结尾的数组修改的最小次数 // // 则状态转移方程为 // // d[i][j] = min(d[i][j],d[i-1][k]+(j,k是否相同或者相邻?0:1)); // // 个人感觉还是比较明显的,最后的答案就是min(d[L]

动态规划——线性dp

我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模型,例如最长上升子序列(LIS).最长公共子序列(LCS).最大子序列和等,那么首先我们从这几个经典的问题出发开始对线性dp的探索. 首先我们来看最长上升子序列问题. 这个问题基于这样一个背景,对于含有n个元素的集合S = {a1.a2.a3……an},对于S的一个子序列S‘ = {ai,aj,ak

dp入门题目

本文文旨,如题... 转载请注明出处... HDOJ 1176 免费馅饼 http://acm.hdu.edu.cn/showproblem.php?pid=1176 类似数塔,从底往上推,每次都是从下面三个中选最大的 1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 100005 4 int dp[MAXN][11];//第i秒第j个位置的馅饼数 5 int max1(int a,int b) 6 { 7 return a

【dp入门题】【跟着14练dp吧...囧】

A HDU_2048 数塔 dp入门题——数塔问题:求路径的最大和: 状态方程: dp[i][j] = max(dp[i+1][j], dp[i+1][j+1])+a[i][j];dp[n][j] = a[n][j]; 其中dp[i][j]: 深度为i的第j个结点的最大和; 1 /* 2 Problem: HDU-2048 3 Tips: Easy DP 4 dp[i][j]: 深度为i的第j个结点的最大和: 5 dp[i][j] = max(dp[i+1][j], dp[i+1][j+1])+

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

HDU 2084 数塔(简单DP入门)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 41852    Accepted Submission(s): 24820 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

uva 11552 Fewest Flops 线性dp

// uva 11552 Fewest Flops // // 二维线性dp // // 首先,在该块必须是相同的来信.首先记录每块有很多种书 // 称为是counts[i]; // // 订购f[i][j]它代表前i字母j为结尾的最小分块数 // // 假设第i块的開始字母与第i-1块的结束字母同样 // f[i][j] = min(f[i][j],f[i-1][k] + counts[i] - 1); // // 否则 // // f[i][j] = min(f[i][j],f[i-1][k

poj3267——线性dp

poj3267——线性dp The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8458   Accepted: 3993 Description Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'