1. 程式人生 > >Common Subsequence (DP最長公共子序列)

Common Subsequence (DP最長公共子序列)

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.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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.
Input
abcfbc abfcab
programming contest 
abcd mnp
Output
4
2
0
Sample Input
abcfbc abfcab
programming contest 
abcd mnp
Sample Output
4
2
0

程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype> 
#include<algorithm>
using namespace std;
const int N=1e3+5;

char a[N],b[N];
int dp[N][N];

int main(void)
{
	int x,y;
	while(~scanf("%s %s",a+1,b+1))
	{
		x=strlen(a+1);
		y=strlen(b+1);
		int i,j;
		memset(dp,0,sizeof(dp));
		for(i=0;i<=x;i++)
		{
			for(j=0;j<=y;j++)
			{
                                //DP核心
				if(i==0||j==0)
				dp[i][j]=0;
				else if(a[i]==b[j])
				{
					dp[i][j]=dp[i-1][j-1]+1;
				}
				else
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		printf("%d\n",dp[x][y]);
	}
	return 0;
}

相關推薦

Common Subsequence DP公共序列

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

Common Subsequence --(LCS:公共序列

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 Subsequence(DP 公共序列

Problem Description   A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a

POJ1458 Common Subsequence —— DP 公共序列LCS

common vector tin enc one 技術分享 com iss char 題目鏈接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS Memory Limi

【HDU】1159 Common SubsequenceDP公共序列

【HDU】1159 Common Subsequence (DP、最長公共子序列) 題目內容 Problem Description A subsequence of a given sequence is the given sequence

POJ 1458 - Common Subsequence公共序列 題解

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

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

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

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

1458:Common Subsequence公共序列

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

dp-公共序列LCS

維數 追加 brush 解決 復雜 long long abcde urn 二維 字符序列 與 字符字串的區別   序列是可以不連續的字符串 , 字串必須要是連續的 。 問題描述 :   給定兩串字符串 abcde 和 acdf , 找出 2 串中相同的字符序列,觀

poj——1159dp公共序列

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

筆試題學習dp,重疊問題,卡特蘭數,手電過橋,公共序列

卡特蘭數:https://blog.csdn.net/doc_sgl/article/details/8880468 dp,重疊子問題:https://www.cnblogs.com/hapjin/p/5572483.html 美團2016校招筆試題:https://zhuanlan.zhihu.com/p/

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

newcoder 練習賽17 B 好位置 dp 公共序列

abc i++ 包含 輸入 http light DC 同時 輸出 鏈接:https://www.nowcoder.com/acm/contest/109/B來源:牛客網 好位置 時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 32768K,

dp-公共序列

    最長公共子序列(NYOJ36) 時間限制: 3000 ms  |  記憶體限制:65535 KB 難度: 3   描述 咱們就不拐彎抹角了,如題,需要

hdoj1160:FatMouse's Speeddp+遞減序列思想+陣列巧妙記錄輸出

目錄 FatMouse's Speed 解題思路: ac程式碼: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276

小樂樂匹配字串 公共序列

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