POJ 1159 最長公共子序列的應用
DP已經弱到不能再弱的程度了。。。。那就從最基本的做起吧!!
看隊友部落格裡面有這題,做了,讀完題,沒思路,才知道,一個叫最長公共子序列的東西不知道(注意與最長公共子串分開)
最長公共子序列作法:
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-1][j],dp[i][j-1]}
本題中的S1和S2是S和S的逆序串
但由於N是5000,需要優化空間,優化空間首先想到滾動陣列,附程式碼:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #define N 5004 #define cl(a) memset(a,0,sizeof(a)) #define ss(a) scanf("%s",a) using namespace std; int f[3][N]; char s[N]; int main() { int n,i,j,e; while (cin>>n) { ss(s); cl(f); e=0; for (i=1;i<=n;i++) { e=1-e; for (j=1;j<=n;j++) { char ch=s[n-j]; if (s[i-1]==ch) f[e][j]=f[1-e][j-1]+1; else f[e][j]=max(f[1-e][j],f[e][j-1]); } } cout<<n-f[e][n]<<endl; } return 0; }
相關推薦
POJ 1159 最長公共子序列的應用
DP已經弱到不能再弱的程度了。。。。那就從最基本的做起吧!! 看隊友部落格裡面有這題,做了,讀完題,沒思路,才知道,一個叫最長公共子序列的東西不知道(注意與最長公共子串分開) 最長公共子序列作法: dp[i][j] 表示s1串中前i個字元,s2串中前j個字元所組成的兩個串的
POJ 1458/HDU 1159 最長公共子序列 (動態規劃)
題目連結:poj && hdu 程式碼 #include <iostream> #include <cstdio> #include <algo
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
POJ-1458 最長公共子序列
這個題的意思就是說: 給一個序列 A 和 B ,讓你求他們的共同的子序列的長度,這些子序列可以不在原來的字串中連續排列。 這個題的話,我們可以使用動態規劃的思路,我們假設 MaxLen [ i ] [ j ] 是 A 串和 B 串中從一開始的,A 串中的的第 i 個字元和B 串中的第 j 個
poj之最長公共子序列和最長公共子串
Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X =
poj 1159 Palindrome(最長公共子序列 + 滾動陣列)
http://poj.org/problem?id=1159 題意:給定一個字串,問最少插入多少個字元,使得該字串變成迴文字串。 思路:原字串序列是X,逆序列是Y,則最少需要補充的字母數=X的長度-X
poj——1159(dp之最長公共子序列)
注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。 #include <iostream> #include <cmath> #include
【poj 1159】 Palindrome DP(類最長公共子序列)+滾動陣列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 58492 Accepted: 20318 Description A palindrom
【mark】最長公共子序列(poj 1458+hdu 1159)
經典的問題,在各大部落格上有數不清的好帖子 下面為最常見的n^2演算法 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #inclu
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
[poj 2274]後綴數組+最長公共子序列
max %d eight har 題目 while color sca 鏈接 題目鏈接:http://poj.org/problem?id=2774 後綴數組真的太強大了,原本dp是n^2的復雜度,在這裏只需要O(n+m)。 做法:將兩個串中間夾一個未出現過的字符接起來,然
POJ 1458 - Common Subsequence(最長公共子序列) 題解
void 方式 mem strong 輸出 inline ron eof init 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=1458 題目大意: 有若幹組數據,每組給出兩個字符
POJ 1458 簡單dp 最長公共子序列
要求:輸出最長公共子序列的長度 方法:dp裸題 1.狀態:dp[i][j]表示第一個序列的前i個字元和第二個序列的前j個字元的最長公共子序列的長度。 2.狀態轉移方程在程式碼中。 3.首要是程式碼規範化,然後才是找bug。 #include<stdio.h> #inc
poj~最長公共子序列和最長公共子串
最長公共子序列 poj1458 問題描述 給出兩個字串,求出這樣的一個最長的公共子序列的長度: 子序列中的每個字元都能在兩個原串中找到, 而且每個字元的先後順序和原串中的先後順序一致。 S
POJ 1458 Common Subsequence(最長公共子序列LCS)
題意: 給你兩個字串, 要你求出兩個字串的最長公共子序列長度. 分析: 本題不用輸出子序列,很簡單,直接處理即可. 首先令dp[i][j]==x表示A串
poj 1934 Trip 多個最長公共子序列
題目要你按字典序輸出兩個字串的多個最長公共子序列 思路: 先用動態規劃求兩個字串的最長公共子序列的儲存在dp[i][j];dp[i][j]表示s1字串1到i和s2字串1到j的最長公共子序列的長度 然後用兩個變數last1[i][j],last2[i][j]來分別儲存字元j
POJ 1458(最長公共子序列)
Time Limit: 1000MS Memory Limit: 10000K Description A subsequence of a given sequence is the given sequence with some elemen
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 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