1. 程式人生 > >POJ 1458(最長公共子序列)

POJ 1458(最長公共子序列)

Time Limit: 1000MS

Memory Limit: 10000K

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

問題分析

題意:求倆個字串的最長公共子串的長度
一道經典dp。
在解決這一類的問題時,我們所需要做的肯定是
⒈找子問題
⒉確定狀態
⒊找出狀態轉移方程
那怎麼找子問題呢?題意要求兩個字串的最長公共子串,不如試著去求s1左邊0->s1.length-1和s2左邊0->s2.length-1的的最長公共部分。也就是在0->s1.length-1和0->s2.length-1中求maxLength(i,j),因此,maxLength(i,j)就是本題的狀態。所以我們可以通過去比較s1[i-1]和s2[j-1],來遞推maxLength(i,j)。所以本題的狀態方程為
if(s1[i-1]==s2[j-1])


maxLength(i,j) = maxLength(i-1,j-1)
else
maxLength(i,j) = max(maxLength(i-1,j),maxLength(i,j-1))
【s1[i-1]!= s2[j-1]時,maxLenth(s1,s2)不會比maxLength(s1,s2j-1) 和MaxLen(s1i-1,s2)兩者之中任何一個小,也不會比兩者都大。】
最後輸出maxLength(s1.length,s2.length)即可。

好,下面上ACcode(^_^)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
const int inf = 0x3f3f3f;
const int N = 500;
using namespace std;

int dp[N][N];

int main()
{
    char a[N],b[N];
    while(~scanf(" %s %s",a ,b))
    {
        int a1 = strlen(a);
        int b1 = strlen(b);
        for(int i = 1; i <= a1; i++)
        {
            for(int j = 1; j <= b1; ++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[a1][b1]<<endl;
    }
    return 0;
}

相關推薦

POJ 1458公共序列

Time Limit: 1000MS Memory Limit: 10000K Description A subsequence of a given sequence is the given sequence with some elemen

POJ 1458 - Common Subsequence公共序列 題解

void 方式 mem strong 輸出 inline ron eof init 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=1458 題目大意: 有若幹組數據,每組給出兩個字符

1458:Common Subsequence公共序列

Common SubsequenceTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 58057Accepted: 24243Descri

HDU 1513 Palindrome:LCS公共序列or 記憶化搜索

ble urn size rom str 個數 blog using reverse 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 題意:   給你一個字符串s,你可以在s中的任意位置添加任意字符,問你將s變成一個回

LCS公共序列

rdquo 工作 dna abc sub 動態規劃 != 給定 似的   這個問題很有意思,在生物應用中,經常需要比較兩個(或多個)不同生物體的DNA片段。例如,某種生物的DNA可能為S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA,S2 = GTCGT

小樂樂匹配字串 公共序列

連結:https://ac.nowcoder.com/acm/contest/301/E 來源:牛客網   小樂樂匹配字串 時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 32768K,其他語言65536K 64bit IO Format: %lld 題目描述

學習筆記--NLP文字相似度之LCS公共序列

最長公共子序列 一個序列S任意刪除若干個字元得到的新序列T,則T叫做S的子序列 兩個序列X和Y的公共子序列中,長度最長的那個,定義為X和Y的最長公共子序列  例如:      --字串12455與245576的最長公共子序列為2455      --字串acd

LCS(longest common subsequence)公共序列演算法模板

看了幾分寫的相當好的部落格: 下面內容來轉載自上面文章 問題描述 什麼是最長公共子序列呢?好比一個數列 S,如果分別是兩個或多個已知數列的子序列,且是所有符合此條件序列中最長的,則S 稱為已知序列的最長公共子序列。     舉個例子,如:有兩條

LCS公共序列問題

例題見挑戰程式設計競賽P56 解釋:子序列是從原序列中按順序(可以跳著)抽取出來的,序列是不連續的,這是其和子串最大的區別; 我們可以定義dp陣列為dp[i][j],表示的是s1-si和t1-ti對應的最長公共子序列長度 狀態轉移方程的話我們分為s[i],t[i]相同和s[i],t[i]不同時的情況

POJ 2217 公共

先考慮一個簡單的問題,計算一個字串中至少出現兩次以上的最長子串,答案一定會在後綴陣列中相鄰兩個字尾的公共字首之中,所有隻要考慮他們就好了,原因是子串的開始位置在後綴中相距越遠,其公共字首的長度也就越短,因此,高度陣列的最大值就是答案。 再考慮這個問題的解法,因為對於兩個字串

HDU 1159:Common Subsequence公共序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23108    Accepted Submission(s

HDU 1159 Common Subsequence公共序列

大概題意:給出兩個字串s1,s2,求他們的最長公共子序列長度... 大概算是模板題? #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&

1159——Common Subsequence 公共序列

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ...,

演算法導論第十五章公共序列

package chapter15_dynamic_programming; import java.util.ArrayList; /** * 動態規劃:最長公共子序列,動態規劃的演算法自底向上的計算 * * @author liuyw * */ pub

POJ 1458 - Common Subsequence公共

strlen cstring algorithm 鏈接 space %d ace -s set 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=1458 AC代碼:

poj——1159dp之公共序列

注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。 #include <iostream> #include <cmath> #include

poj 1159 Palindrome公共序列+滾動陣列

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to writ

1045 Favorite Color Stripe 30 分公共序列變形

1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in

10723 公共序列變形

思路:求長度最少的串的思想和求最長公共子串基本一致,dp[i][j]即可。 求數量,則藉助於前面的dp[i][j]。 具體思路看程式碼: #include <cstdio> #include <cstring> #include <algo

Prince and Princess公共序列優化,動態規劃

<a target=_blank href="https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&