傳送門
思路:經典兩個字串匹配的dpdp
dp,找到狀態轉移方程即可。
這裡提供兩種寫法,dpdp
dp,記憶化搜尋。實質一樣。
p s:
ps:ps
:記憶化搜尋看著舒服一些。。
d pdp
dp**:
#include
using
namespace std;
typedef
long
long ll;
const
int n=
2e3+5;
#define mst(a) memset(a,0,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define pii pair
#define fi first
#define se second
inline
void
read
(int
&x)for
(;ch>=
'0'&&ch<=
'9';ch=
getchar()
) x=
(x<<3)
+(x<<1)
+(ch&15)
; x*
=w;}
int dp[n]
[n];
intmain()
);printf
("%d\n"
,dp[la]
[lb]);
return0;
}
記憶化搜尋**:
#include
using
namespace std;
typedef
long
long ll;
const
int n=
2e3+5;
#define mst(a) memset(a,0,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define pii pair
#define fi first
#define se second
inline
void
read
(int
&x)for
(;ch>=
'0'&&ch<=
'9';ch=
getchar()
) x=
(x<<3)
+(x<<1)
+(ch&15)
; x*
=w;}
int dp[n]
[n],n;
char a[n]
,b[n]
;int
dfs(
int i,
int j));
}int
main()
P2758 編輯距離 dp
設a和b是兩個字串。我們要用最少的字元操作次數,將字串a轉換為字串b。這裡所說的字元操作共有三種 1 刪除乙個字元 2 插入乙個字元 3 將乙個字元改為另乙個字元 皆為小寫字母!第一行為字串a 第二行為字串b 字串a和b的長度均小於2000。只有乙個正整數,為最少字元操作次數。輸入 1複製 sfdq...
P2758 編輯距離 簡單DP
要把兩個字串變成相同的字串 一共可以利用以下三種操作 1 把串中任意乙個字元刪去 2 在串中任意乙個位置插入乙個字元 3 把串中任意乙個字元變成其他任意字元 問至少要多少次操作才能讓兩個字串相同?狀態 設 f i j 為把 s1 0 s1 i 1 變為 s2 0 s2 j 1 所需要的步數。對於 f...
P2758 編輯距離
p2758 編輯距離 設a和b是兩個字串。我們要用最少的字元操作次數,將字串a轉換為字串b。這裡所說的字元操作共有三種 1 刪除乙個字元 2 插入乙個字元 3 將乙個字元改為另乙個字元 皆為小寫字母!輸入格式 第一行為字串a 第二行為字串b 字串a和b的長度均小於2000。輸出格式 只有乙個正整數,...