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

思路:题目的意思是给定两个字符串,求他们的最长公共子序列,其中子序列是可以不连续的。该题目是典型的动态规划问题,令dp[i][j]表示s1的前i个字符与s2的前j个字符的最长公共子序列,则当s1[i]==s2[j]时,dp[i][j]=dp[i-1][j-1]+1,如果不等,则dp[i][j]=max{dp[i][j+1],dp[i+1][j]},具体代码如下:

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int LCS(string s1,string s2)
{
	int length1 = s1.size(),length2 = s2.size(),i,j;
	vector<vector<int> > dp(length1+1);
	for(i = 0;i <= length1;++i)
	{
		vector<int> tmp(length2+1,0);
		dp[i] = tmp;
	}
	for(i = 0;i < length1;++i)
	{
		for(j = 0;j < length2;++j)
		{
			if(s1[i] == s2[j])dp[i+1][j+1] = dp[i][j]+1;
			else dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
		}
	}
	return dp[length1][length2];
}

int main()
{
	string s1,s2;
	while(cin >> s1 >> s2)
	{
		cout << LCS(s1,s2) << endl;
	}
	return 0;
}

poj之最长公共子序列

时间: 2024-12-08 02:13:29

poj之最长公共子序列的相关文章

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

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

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 3356 AGTC(最长公共子序列)

AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missin

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

POJ 2127 最长公共上升子序列

动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #inc

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)

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[