[luoguP2758] 编辑距离(DP)

传送门

f[i][j] 表示第一串前 i 个到第二串前 j 个的最小编辑距离

f[i][j] = f[i - 1][j - 1] (s1[i] == s2[j])

f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) (s1[i] != s2[j])

边界 f[0][j] = j (1 <= j <= m)

   f[i][0] = i (1 <= i <= n)

——代码

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 int n, m;
 5 int f[2048][2048];
 6 char s1[2048], s2[2048];
 7
 8 inline int min(int x, int y)
 9 {
10     return x < y ? x : y;
11 }
12
13 int main()
14 {
15     int i, j;
16     scanf("%s %s", s1 + 1, s2 + 1);
17     n = strlen(s1 + 1);
18     m = strlen(s2 + 1);
19     for(i = 1; i <= n; i++) f[i][0] = i;
20     for(i = 1; i <= m; i++) f[0][i] = i;
21     for(i = 1; i <= n; i++)
22         for(j = 1; j <= m; j++)
23         {
24             if(s1[i] == s2[j]) f[i][j] = f[i - 1][j - 1];
25             else f[i][j] = min(min(f[i - 1][j], f[i][j - 1]), f[i - 1][j - 1]) + 1;
26         }
27     printf("%d\n", f[n][m]);
28     return 0;
29 }

时间: 2024-07-31 14:21:26

[luoguP2758] 编辑距离(DP)的相关文章

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

poj3356 字符串的最小编辑距离 dp

poj3356 字符串的最小编辑距离  dp AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10895   Accepted: 4188 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 bel

Codeforces 67C Sequence of Balls 编辑距离 dp

题目链接:点击打开链接 有一个交换操作比较特殊,所以记录每个点距离自己最近的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一行的2个字符之间 然后再进行交换. #include <cstdio> #include <cstring> #include<iostream> using namespace std; #define inf 10000000 #define N 4005 #define

POJ 3356 AGTC (编辑距离 DP)

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

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru

HDU 4323 Magic Number(编辑距离DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个串可以转化为询问中给的串. 思路: 明显的编辑距离DP,直接暴力过了,网上有用bk树的,可惜我不会. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6

51nod 1183 编辑距离(dp)

题目链接:51nod 1183 编辑距离 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 1001; 6 char a[N], b[N]; 7 int dp[N][N];//dp[i][j]:a串的前i个字符转化成b串的前j个字符的最少操作数 8 int main(){ 9 int i, j; 10 scanf(&quo

LeetCode:Edit Distance(字符串编辑距离DP)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

Luogu-P2758 编辑距离

题目 题目链接 测试得分: 100 主要算法 : 动态规划,区间DP,字符串 题干: 区间字符串DP板子题 应试策略: 确定算法区间DP 确定状态//f[i][j]字符串sta(sta[0]-sta[i-1])转化到字符串stb(stb[0]-stb[j-1])的最短编辑距离 初始状态与边界 将sta删除i个:f[i][0](0<=i<=n),将stb插入j个:f[0][j](0<=j<=m) 状态转移: 当sta[i-1]==stb[j-1]时,f[i][j]=fi-1][j-1