1. 程式人生 > >【水:最長公共子序列】【HDU1159】【Common Subsequence】

【水:最長公共子序列】【HDU1159】【Common Subsequence】

Common Subsequence

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


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

Sample Input abcfbc abfcab programming contest abcd mnp
Sample Output 4 2 0 題目沒看完 看了一下樣例 估計是最長公共子序列。。 雖然水題  還是記不牢 寫一遍
#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <cstring>  
#include <ctime>  
#include <algorithm>  
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313   
using namespace std;
char A[1001],B[1001];
int F[1000][1000];
void solve()
{
		memset(F,0,sizeof(F));
		int lena=strlen(A);
		int lenb=strlen(B);
		F[0][0]=0;
		F[0][1]=0;
		F[1][0]=0;
		for(int i=1;i<=lena;i++)
		 for(int j=1;j<=lenb;j++)
	{
		 if(A[i-1]==B[j-1])
		 F[i][j]=F[i-1][j-1]+1;
		 else F[i][j]=max(F[i][j-1],F[i-1][j]);
	}
		printf("%d\n",F[lena][lenb]);
}
int main()
{
	while(scanf("%s %s",A,B)!=EOF)
	{
		solve();
	}
	return 0;
}
  


相關推薦

公共序列HDU1159Common Subsequence

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24919    Acc

hdu1159-Common Subsequence(DP公共序列LCS)

dice com main sizeof accept pan nbsp any ++ Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (

51Nod1006公共序列Lcs

https://www.51nod.com/Challenge/Problem.html#!#problemId=1006 給出兩個字串A B,求A與B的最長公共子序列(子序列不要求是連續的)。 比如兩個串為:   abcicba abdkscab  

三維一邊推公共序列加強版(三串LCS) CAIOJ - 1073 dp lcs

題解 與二位lcs類似 列舉三個串的每個位置 狀態轉移考慮5種情況 abc當前位置全相等則由3個串長度全-1的位置轉移過來 lcs+1 ab相等但不與c相等 則由ab長度都-1或c長度-1取max轉移過來 ac相等但不與b相等和bc相等但不與a相等類似 abc互不相等則由a、b或c長度-

演算法學習——動態規劃 例題公共序列問題(java)

題目: 給定兩個字串str1和str2,返回兩個字串的最長公共子序列.例如,str1="1A2C3D4B56",str2="B1D23CA45B6A","123456"或者"12C4B6' 動態規劃思想: 先用一個比,左邊加一個字元右面加一個字元依次比較dp[i][j] dp[i][j]意思

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

類似於字典序比較的最長公共子序列, 只要找到狀態方程就比較好搞, 從角標1開始計算不會出錯,從0還是報錯了; 切防止對比溢位要在本來的基礎上+1計算; 言歸正傳: DP型別都是狀態方程很重要; 假設兩

HDU 1159 基礎DP 公共序列

TAT,,,覺得自己不算太理解,但是居然可以憑著記憶做出來。。蠻拼的 #include <cstdio> #include <cstring> #include <iostream> #include <string> usi

經典問題二維動態規劃問題公共序列LCS

原博地址:http://blog.csdn.net/yysdsyl/article/details/4226630                 http://blog.csdn.net/ljyljyok/article/details/77905681    證明:   

HackerRankCommon Child (LCS)公共序列

lin ring def imp sep content hat jin ted Given two strings a and b of equal length, what’s the longest string (S) that can be construct

DP公共序列

amp 給定 scrip ros script print 最長 去掉 != Description   字符序列的子序列是指從給定字符序列中隨意地(不一定連續)去掉若幹個字符(可能一個也不去掉)後所形成的字符序列。令給定的字符序列X=“x0,x1,…,xm-1”,序列Y

公共序列--算法導論

pan end art blog src http size ret bdc 最長公共子序列:一個序列 S 。假設各自是兩個或多個已知序列的子序列,且是全部符合此條件序列中最長的,則 S 稱為已知序列的最長公共子序列。 其核心非常easy: 這樣,構造子結構就比較簡

動態規劃公共序列問題

clas == 搜索 ios for 參考 pan 公式 是否 題目描述: 給定兩個字符串s1s2……sn和t1t2……tn。求出這兩個字符串最長的公共子序列的長度。字符串s1s2……sn的子序列指可以表示為si1si2……sim(i1<i2<……<im)

HDU 1513 PalindromeLCS(公共序列)or 記憶化搜索

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

51nod 1183 編輯距離線性dp+類似公共序列

ima else ems 俄羅斯 hid ace mem std 類型 1183 編輯距離 基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 收藏 關註 編輯距離,又稱Levenshtein距離

例9.9公共序列

std 不存在 cst i++ 公共子串 ostream ont enter http 【例9.9】最長公共子序列 鏈接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1265 時間限制: 1000 ms 內

bzoj2423[HAOI2010]公共序列 dp

代碼 ring ont 註意 return 需要 sam tdi light 題目描述 字符序列的子序列是指從給定字符序列中隨意地(不一定連續)去掉若幹個字符(可能一個也不去掉)後所形成的字符序列。令給定的字符序列X=“x0,x1,…,xm-1&

洛谷 P1439 模板公共序列

clu () 休閑 一句話 中一 AD DC == c++ 神TM模板。。我本來想休閑一下寫點水題的。。。 開始做的時候直接敲了一個O(N2)的算法上去,編譯的時候才發現根本開不下。。 好了,談回這道題。 先不加證明的給出一種算法。 若有一組數據 2 4 2 5 1 3

bzoj 2423: [HAOI2010]公共序列dp+計數

class pri ace 滾動數組 spa == i++ int const 設f[i][j]為a序列前i個字符和b序列前j個字符的最長公共子序列,轉移很好說就是f[i][j]=max(f[i-1][j],f[i][j-1],f[i-1][j-1]+(a[i]==b[j]

luogu P1439 模板公共序列

bsp 第一個 順序 它的 amp while ++ return scan 題目qwq (第一道藍題) 據說是用了hash的思想(?) 總之是先把第一個序列每個數出現的順序記下來(其實第一個序列的數字不用記), 然後第二個序列的每個數都對照它的順序,這樣只要得到一個升序的