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];j++)
            {
                if (dp[j]>Max) Max=dp[j];
                else
                if (a[j]==b[i])
                {
                    if (dp[j]<Max+1) dp[j]=Max+1;
                }

            }
        }
        Max=0;
        for (i=0;a[i];i++)
            if (dp[i]>Max) Max=dp[i];
        printf("%d\n",Max);
    }
    return 0;
}

POJ 1458 LCS模板

时间: 2024-10-07 15:08:45

POJ 1458 LCS模板的相关文章

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 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

[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];

LCS模板,求长度,并记录子串

1 //LCS模板,求长度,并记录子串  2 //亦可使用注释掉的那些代码,但所用空间会变大 3 #include<iostream> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<cstdio> 8 using namespace std; 9 #define N 5005 10 11 int len[N][N]; 12 char str1[N],str

hdu1159 LCS模板题

题目分析 原题地址 最简单的最长公共子序列(LCS)问题的模板题了.不解释. ------------------------------------------------------------------------ 状态转移方程: dp[i][j]=dp[i-1][j-1]+1                                 (a[i-1]==b[j-1]) dp[i][j]=max(dp[i-1][j],dp[i][j-1] )              (a[i-1]

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 1113 凸包模板题

上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #include <queue> #include <map> #include

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

POJ 1458 Common Subsequence(最长公共子序列LCS)

POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[