poj 1458 Common Subsequence(dp)

Common Subsequence

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46630   Accepted: 19154

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

Java AC 代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String first = "";
        String second = "";
        while(!(first = sc.next()).equals("") && !(second = sc.next()).equals("")) {
            char[] firstArray = first.toCharArray();
            char[] secondArray = second.toCharArray();
            int firstLen = first.length();
            int secondLen = second.length();
            int[][] subMaxLen = new int[firstLen + 1][secondLen + 1];  //这里设置成长度加1的,是为了防止下面 i-1 j-1的时候数组越界。
            for(int i = 1; i <= firstLen; i++)
                for(int j = 1; j <= secondLen; j++) {
                    if(firstArray[i - 1] == secondArray[j - 1])
                        subMaxLen[i][j] = subMaxLen[i - 1][j - 1] + 1;
                    else
                        subMaxLen[i][j] = (subMaxLen[i - 1][j] > subMaxLen[i][j - 1] ? subMaxLen[i - 1][j] : subMaxLen[i][j - 1]);
                }
            System.out.println(subMaxLen[firstLen][secondLen]);
        }

    }

}
时间: 2024-11-06 18:43:31

poj 1458 Common Subsequence(dp)的相关文章

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

题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串LCS长度. 当A[i] = B[j] 时,这个最长度就是上一个长度加1,即:d(i, j) = d(i-1, j-1) + 1; 当A[i] != B[j] 时,那就是前面的最长长度(因为即使后面的不成立,也不会影响前面的),即:d(i, j) = max{d(i-1, j), d(i, j-1)}

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

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

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39128   Accepted: 15770 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 1458 - Common Subsequence(最长公共子串)

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=1458 AC代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 using namespace std; 7 int ans,lena,lenb,f[201

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 Common Subsequence (LCS最长公共子序列)

题意: 给出两个字符串,求出最长的公共子序列大小. 思路: 算是最经典的LCS问题吧. 设 \(X=(x_1,x_2,.....x_n) 和 Y=(y_1,y_2,.....y_m)\) 是两个序列,将 X 和 Y 的最长公共子序列记为\(lcs(X,Y)\) ,找出\(lcs(X,Y)\)就是一个最优问题 然后我们需要将其分解成子问题并求出子问题的最优解: (寻找子问题来推出当前问题,正是寻找状态转移方程最重要的一步) 1)如果 \(x_n=y_m\),即X的最后一个元素与Y的最后一个元素相同

poj 1458 Common Subsequence ——(LCS)

虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 const int N = 1000 + 5; 6 7 char a[N],b[N]; 8 int dp[N][N]; 9 10 int main() 11 { 12 while(scanf("

poj 1458 Common Subsequence(最大公共子序列)

#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define INF 30000000 #define pai 3.1415926 using namespace std; char s1[1000],s2[1000]; int dp[1000][1000]; int main() { int i,j,k; whi