Codeforces 56D Changing a String 编辑距离 记忆化dp
来源:互联网 发布:网络兼职写小说 编辑:程序博客网 时间:2024/06/05 17:10
题目链接:点击打开链接
编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好
#include<iostream>#include<cstdio>#include<vector>#include<string.h>using namespace std;#define ll int#define N 1010char s[N], t[N];int dp[N][N], n, m;// 0为插入 1为删除 2 3为替换struct node{int op;int pos; char c;node(int A=0,int E=0,char D=0):op(A),pos(E), c(D){}void put(){if(op== 2)printf("REPLACE %d %c\n", pos, c);else if(op == 0)printf("INSERT %d %c\n", pos +1, c);else if(op == 1)printf("DELETE %d\n", pos);}}P;void dfs(int a, int b){if(a == 0 && b==0)return ;if(s[a] == t[b] && dp[a-1][b-1] == dp[a][b])dfs(a-1, b-1);else{if(a && dp[a-1][b] +1 == dp[a][b]){P = node(1, a);P.put();dfs(a-1, b);}else if(b && dp[a][b-1] +1 == dp[a][b]){P = node(0, a, t[b]);P.put();dfs(a, b-1);}else if(a && b && dp[a-1][b-1] +1 == dp[a][b]){P = node(2, a, t[b]);P.put();dfs(a-1, b-1);}}}void input(){scanf("%s", t+1);n = strlen(s+1);m = strlen(t+1);}int main(){int i, j;while(~scanf("%s", s+1)){input();dp[0][0] = 0;for(i = 1; i <= n; i++)dp[i][0] = i;for(i = 1; i <= m; i++)dp[0][i] = i;for(i = 1; i <= n; i++)for(j = 1; j <= m; j++)dp[i][j] = min(min(dp[i-1][j], dp[i][j-1])+1, dp[i-1][j-1] + (s[i]!=t[j]));printf("%d\n", dp[n][m]);dfs(n, m);}return 0;}
0 0
- Codeforces 56D Changing a String 编辑距离 记忆化dp
- codeforces 56D Changing a String (dp编辑距离)
- D. Changing a String (编辑距离)(dp+记忆化)(dp后寻找路径大法)
- codeforces 56D Changing a String 区间dp,字符串
- Codeforces Beta Round #52 (Div. 2) D - Changing a String
- codeforces 148D 概率dp 记忆化搜索
- codeforces--55D--Beautiful numbers(数位dp,dfs+记忆化)
- Codeforces 793D Presents in Bankopolis【Dp+记忆化搜索】
- Codeforces Beta Round #11, problem: (D) A Simple Task 状态压缩DP+记忆化搜素DP
- POJ3373:Changing Digits(记忆化)
- Codeforces 67C Sequence of Balls 编辑距离 dp
- codeforces 82D Two out of Three DP 记忆化搜索
- Codeforces Div.301D Bad Luck Island(概率dp+记忆化搜索)
- Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)
- Codeforces Round #106(Div. 2) 149D. Coloring Brackets 区间DP 记忆化搜索
- CodeForces 149D Coloring Brackets(记忆化搜索区间dp)【多做】
- codeforces 149D Coloring Brackets (区间DP OR 记忆化搜索 总结!!!)
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
- (7)Android之Activity学习
- First Seal
- 设计模式2—Proxy设计模式
- 用“删除重复项”提取出现值
- ios数组基本用法和排序
- Codeforces 56D Changing a String 编辑距离 记忆化dp
- 数字电路设计之写verilog易犯的错误
- VC 线程同步实例
- (队列)queue
- hdu 1130 How Many Trees
- prepareStatement与Statement的区别
- 杭电 1069 Monkey and Banana
- 配置域名映射
- Java内部类解析及其运用