POJ-1458 LCS(线性动态规划)

此题经典线性动态规划。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=505;
int d[maxn][maxn];
int main(void){
    string a,b;
    while(cin>>a>>b){
        memset(d,0,sizeof(d));
        for(int i=1;i<=a.length();i++){
            for(int j=1;j<=b.length();j++){
                if(a[i-1]==b[j-1]){
                    d[i][j]=d[i-1][j-1]+1;
                }
                else{
                    d[i][j]=max(d[i][j-1],d[i-1][j]);
                }
            }
        }
        cout<<d[a.length()][b.length()]<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/KYSpring/p/8735181.html

时间: 2024-08-21 19:07:42

POJ-1458 LCS(线性动态规划)的相关文章

POJ 1458 LCS模板

LCS模板 存一个 #include "stdio.h" #include "string.h" int main() { char a[1010],b[1010]; int i,j,Max,dp[1010]; while (scanf("%s",a)!=EOF) { scanf("%s",b); memset(dp,0,sizeof(dp)); for (i=0;b[i];i++) { Max=0; for (j=0;a[j

POJ 1458 LCS 数组过小因编译器不同引发

按道理说LCS的问题应该讨论的很明白了,不应该出问题.昨天晚上手贱点开了暑期写的LCS滚动数组的代码.发现毫无逻辑错误. 但却是WA,用的C++,.于是随手换了个g++ 却手动把1-flag 与flag相比较输出最大,就AC #include<stdio.h> #include <math.h> #include <string.h> #define N 2000 char str1[N]; char str2[N]; int dp[2][N]; int main()

poj 1458 动态规划DP

//  poj 1458  zoj 1733  最长公共子序列  DP #include <iostream>#include <string.h>#define N 1005using namespace std ;char  s1[N],s2[N];   int dp[N][N];int max(int a,int b)   {    return a>b ? a : b ;  }void f(int n,int m){   int i,j;    for (i=0; i

Poj 1458 Common Subsequence(LCS)

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

POJ 1458 Common Subsequence (动态规划)

题目传送门 POJ 1458 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 ex

[2016-03-28][POJ][1458][Common Subsequence]

时间:2016-03-28 12:56:39 星期一 题目编号:[2016-03-28][POJ][1458][Common Subsequence] 题目大意:最长公共序列 #include <cstring> #include <iostream> #include <string> using namespace std; typedef long long LL; const int maxn = 1000 + 100; int dp[maxn][maxn];

POJ 3071 Football (动态规划-概率DP)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2768   Accepted: 1412 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the

动态规划②——线性动态规划(背包)

二.线性动态规划 上次只能算一个热身,这次是真正的动态规划了.还是洛谷(http://www.luogu.org/problem/show?pid=1048):题目描述 Description辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值.我会给你一段时间,在这段时间里,你可以采到一些

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33363   Accepted: 10330 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o