周赛F题 POJ 1458(最长公共子序列)

F - F

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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, x ij = 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

题解:给你两个字符串,找到他们最长的公共子序列。这个题做过很多次,虽然加了点东西,还是没有写对,还是对LCS理解不够。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[1100],b[1100];
int dp[1100][1100];
int main()
{
    while(scanf("%s %s",a,b)!=EOF)
    {
        int x=strlen(a);
        int y=strlen(b);
        for(int i=1;i<=x;i++)
        {
            for(int j=1;j<=y;j++)
            {
                if(a[i-1]==b[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout<<dp[x][y]<<endl;
    }
    return 0;
}
时间: 2024-08-04 16:16:08

周赛F题 POJ 1458(最长公共子序列)的相关文章

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], j, dp[i, j - 1]); 设有字符串X和字符串Y,dp[i, j]表示的是X的前i个字符与Y的前j个字符的最长公共子序列长度. 如果X[i] == Y[j] ,那么这个字符与之前的LCS 一定可以构成一个新的LCS: 如果X[i] != Y[j] ,则分别考察 dp[i  -1][j

POJ 1458 最长公共子序列

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

poj之最长公共子序列

题目:poj 1458   Common Subsequence 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 subsequenc

实习生面试--算法题之字符串最长公共子序列长度

题目:求两字符串的最长公共子序列的长度. 题外话:最长公共子串,子序列问题是被充分讨论的问题,网上一搜一大把,请bing之. 本题只要求求最长公共子序列的长度,而不需要记录最长的公共子序列,给予了我们便利,请参考代码: 1 int max(int a, int b) 2 { 3 return a > b ? a : b; 4 } 5 6 int lcs(char* str1, char* str2) 7 { 8 if (str1 == nullptr || str2 == nullptr) 9

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

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa

Human Gene Functions POJ 1080 最长公共子序列变形

Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their

POJ 1458 - Common Subsequence(最长公共子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=1458 题目大意: 有若干组数据,每组给出两个字符串(中间用任意数量的空格间隔),输出这两个字符串最长公共子序列的长度.每次输出后换行. 分析: 动态规划求LCS,f[i][j]表示第一个字符串匹配到第i位,第二个字符串匹配到第j位时最长公共子序列的长度. 转移方程:当a[i] = b[i]时,f[i][j] = f[i-1][j-1]+1,其他情况时f[i][j

poj 最长公共子序列 1458 记忆式搜索

#include <iostream>using namespace std;#include<cstring>#define N 1005char s1[N],s2[N];int dp[N][N];int max(int a,int b) {  return a>b ? a:b ;} int f(int x ,int y){ if(dp[x][y]!=-1) return dp[x][y]; if(x==0||y==0)    return dp[x][y]=0; else

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[