POJ1458 Common Subsequence 【最长公共子序列】

Common Subsequence

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37614   Accepted: 15058

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.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

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

NYOJ同题

#include <stdio.h>
#include <string.h>
#define maxn 1000

char str1[maxn], str2[maxn];
int dp[maxn][maxn];

int max(int a, int b){ return a > b ? a : b; }

int LCS()
{
    int ans = 0;
    for(int i = 1, j; str1[i]; ++i){
        for(j = 1; str2[j]; ++j){
            if(str1[i] == str2[j]){
                dp[i][j] = dp[i-1][j-1] + 1;
            }else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            if(dp[i][j] > ans) ans = dp[i][j];
        }
    }
    return ans;
}

int main()
{
    while(scanf("%s%s", str1 + 1, str2 + 1) == 2){
        printf("%d\n", LCS());
    }
    return 0;
}
时间: 2024-10-03 13:20:54

POJ1458 Common Subsequence 【最长公共子序列】的相关文章

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[

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of

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

问题描述 最长公共子序列,英文缩写为LCS(Longest Com #include <bits/stdc++.h> const int MAX=1010; char x[MAX]; char y[MAX]; int DP[MAX][MAX]; int b[MAX][MAX]; using namespace std; int PRINT_LCS(int b[][MAX],char *x,int i,int j) { if(i==0||j==0) return 1; if(b[i][j]==1

POJ 1458 Common Subsequence 最长公共子序列

题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<iostream> #include<algorithm> #define INF 0x3f3f3f3f #define MAXSIZE 10

LCS修改版(Longest Common Subsequence 最长公共子序列)

题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉任意多个字母,使得两句话剩余的部分相同,通过一定的删除手法,可以让剩余的部分相同且长度最大,就得到了可能的暗号.暗号可能有多个,还要进行筛选,现在情报局人手不够,希望你能助一臂之力,筛选工作不用你完成,你只需计算出暗号长度以及个数即可.(注意,字母的位置也是暗号的重要信息,位置不同的字母组成的暗号不

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

LCS 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define clc(a,b) memset(a,b,sizeof(a)) 6 #define LL long long 7 #include<cmath> 8 using namespace std; 9 int dp[1010][1010];//表示到i-1,j-1的

hdu 1159 Common Subsequence(最长公共子序列 动态规划)

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

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

#include<bits/stdc++.h> using namespace std; int dp[1024][1024]; int main() { int i,j; char s1[1024],s2[1024]; while(~scanf("%s %s",s1,s2)) { memset(dp,0,sizeof(dp)); int len1=strlen(s1); int len2=strlen(s2); for(i=0;i<len1;i++) { for(j

Common Subsequence--poj1458(最长公共子序列)

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43211   Accepted: 17526 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..

hdu 1159 common sequence (最长公共子序列 dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意 : 给出两个字符串 求出最长公共子序列 思路: if(str1[i]==str2[j]) { dp[i][j]=max(dp[i-1][j-1]+1,max(dp[i-1][j],dp[i][j-1])); } else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); #include<cstdio> #include<cstring> #include&l