最長公共子序列滾動陣列寫法
如poj1159,普通寫法會mle
滾動陣列可以解決,原因在於普通的dp寫法雖然一層套一層,但是實際上僅僅使用了該層和上一層,所以可以據此優化。
PS.解釋圖片來自網路,圖片原本來自演算法導論
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int main() { string a,b; while(cin>>a>>b) { int len1=a.size(); int len2=b.size(); int e[2][max(len1,len2)+5]; memset(e,0,sizeof(e)); int now=0; for(int i=0; i<=len1; i++) { for(int j=0; j<=len2; j++) { if(i==0||j==0) e[(now+1)%2][j]=0; else if(a[i-1]==b[j-1]) e[(now+1)%2][j]=e[now][j-1]+1; else e[(now+1)%2][j]=max(e[(now+1)%2][j-1],e[now][j]); } now=(now+1)%2; if(i==len1) cout<<e[now][len2]<<endl; } } }
相關推薦
最長公共子序列滾動陣列寫法
如poj1159,普通寫法會mle滾動陣列可以解決,原因在於普通的dp寫法雖然一層套一層,但是實際上僅僅使用了該層和上一層,所以可以據此優化。PS.解釋圖片來自網路,圖片原本來自演算法導論#include<iostream> #include<cstdio&
poj 1159 Palindrome(最長公共子序列 + 滾動陣列)
http://poj.org/problem?id=1159 題意:給定一個字串,問最少插入多少個字元,使得該字串變成迴文字串。 思路:原字串序列是X,逆序列是Y,則最少需要補充的字母數=X的長度-X
【poj 1159】 Palindrome DP(類最長公共子序列)+滾動陣列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 58492 Accepted: 20318 Description A palindrom
1159 Palindrome(迴文串&LCS最長公共子序列&滾動陣列)
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
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
HDU:1513 Palindrome(迴文字串+最長公共子序列+滾動陣列)
Palindrome Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5246 Accepted Subm
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 ar
hdu1513Palindrome(動態規劃之最長公共子序列變形+滾動陣列)
2題意: 就是填多少字元使之變成迴文字串;#include<iostream> #include<cstring> #include<queue> #include<cstdio> #include<string.h> #include<al
hdu1159(最長公共子序列,滾動陣列)
狀態轉移方程為:dp[i][j]=dp[i-1][j-1]+1(s1[i]==s2[j]);dp[i][j]=max(dp[i-1][j],dp[i][j-1])(s1[i]!=s2[j]) 此題字串的長度不是很大,所以不會超記憶體 程式碼如下: #include<i
1060 Nearest Sequence (三個陣列的最長公共子序列)
Description Do you remember the "Nearest Numbers"? Now here comes its brother:"Nearest Sequence".Given three sequences of char,tell me the lengt
] 找工作知識儲備(2)---陣列字串那些經典演算法:最大子序列和,最長遞增子序列,最長公共子串,最長公共子序列,字串編輯距離,最長不重複子串,最長迴文子串
作者:寒小陽 時間:2013年9月。 0、前言 這一部分的內容原本是打算在之後的字串或者陣列專題裡面寫的,但看著目前火熱進行的各家網際網路公司筆試面試中,出現了其中的一兩個內容,就隨即將這些經典問題整理整理,單寫一
最長公共子序列LCS (DP)
mem main amp code bcd max std pan ems 題意: 求兩個字符串的公共子序列,如“abcd” 與 “becd”的公共子序列是 “bcd” 分析: 設兩個字符串為 串s 和串tdp[i][j]:= s1..si和t1...tj對應的LCS長度
【HackerRank】Common 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
最長公共子序列(LCS)
公共子序列 一個 clas style == ++ 字符串 tro 我們 最長公共子序列: LIS是一個典型的用動規解決的問題。 給出兩個字符串,求出兩串的最長公共子序列的長度。 我們可以構造出他的結構特征。f(i,j)表示str1[1]~str1[i]和str2[1]~s
最長公共子序列
pac str 描述 pid scan div gre max ems 1619: P1050 時間限制: 1 Sec 內存限制: 128 MB提交: 38 解決: 28[提交][狀態][討論版] 題目描述 一個字符串A的子串被定義成從A中順次選出若幹個字符構成的串
[C++] 動態規劃之矩陣連乘、最長公共子序列、最大子段和、最長單調遞增子序列
每次 種子 () return 避免 amp 可能 text com 一、動態規劃的基本思想 動態規劃算法通常用於求解具有某種最優性質的問題。在這類問題中,可能會有許多可行解。每一個解都對應於一個值,我們希望找到具有最優值的解。 將待求解問題分解成若幹個子問題,先求
動態規劃 最長公共子序列
一個 then mda 偽代碼 n-2 msu csdn static 證明 最長公共子序列(LCS)問題 下面通過一個具體的例子來學習動態規劃方法 —— 最長公共子序列問題。 最長公共子串(Longest Common Substring)與最
Human Gene Functions POJ 1080 最長公共子序列變形
cee diff print bmi ces -s compare %d determine Description It is well known that a human gene can be considered as a sequence, consisting
最長公共子序列--【算法導論】
pan end art blog src http size ret bdc 最長公共子序列:一個序列 S 。假設各自是兩個或多個已知序列的子序列,且是全部符合此條件序列中最長的,則 S 稱為已知序列的最長公共子序列。 其核心非常easy: 這樣,構造子結構就比較簡