主题链接:点击打开链接

编辑距离。,== 一边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为替换
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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces 56D Changing a String 编辑距离 记忆dp的更多相关文章

  1. Codeforces 56D Changing a String (DP)

    题意:你可以对字符串s进行3种操作: 1,在pos位置插入字符ch. 2,删除pos位置的字符. 3,替换pos位置的字符为ch. 问最少需要多少次操作可以把字符s变成字符s1? 思路: 设dp[i] ...

  2. Codeforces 56D Changing a String

    http://codeforces.com/contest/56/problem/D 题目大意: 一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数. 思路:dp[i][j]代表 ...

  3. 2018.12.12 codeforces 931E. Game with String(概率dp)

    传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...

  4. Codeforces Hello 2018 E题Logical Expression dp+最短路 好题

    j题目链接: http://codeforces.com/contest/913/problem/E 题意: 给你x,y,z三个变量,与&   或|  非!  括号()   四种运算符,规定括 ...

  5. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  6. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  7. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  8. leetcode 72.编辑距离(dp)

    链接:https://leetcode-cn.com/problems/edit-distance/submissions/ 设dp[i][j]表示串s1前i个字符变换成串s2前j个字符所需要的最小操 ...

  9. Codeforces 148D Bag of mice:概率dp 记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...

随机推荐

  1. Codeforces Round #198 (Div. 2) B. Maximal Area Quadrilateral

    B. Maximal Area Quadrilateral time limit per test 1 second memory limit per test 256 megabytes input ...

  2. AWS要进入中国了

    去年底就开始有这个传言见诸于某些媒体,说网站支持中文了要进中国了,也有说这不代表什么是谣言 这几天又听到有消息说比较靠谱就半年内的事儿 其实如果没有政策阻碍的话进中国是迟早的事情,原因如下 Amazo ...

  3. [Swust OJ 85]--单向公路(BFS)

    题目链接:http://acm.swust.edu.cn/problem/0085/ Time limit(ms): 5000 Memory limit(kb): 65535   Descriptio ...

  4. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  5. Js 30 BOM

    小知识点, 1.document.write()方法: 如果document.write()在一个事件中或window.onload=function(){}这个function里, 那么docume ...

  6. HDU 3974 Assign the task 简单搜索

    根据Rex 的思路才知道可以这么写. 题目意思还是很好理解的,就是找到当前雇员最近的任务. 做法是,可以开辟一个 tim 变量,每次有雇员得到昕任务时候 ++tim 然后取寻找最近的任务的时候写一个搜 ...

  7. 模拟Struts2的AOP实现

    在Struts2中有拦截器的概念,通过它的拦截器可以拦截Action.Struts2的拦截器是通过AOP来实现的,在Spring也有类似的概念.下面的我们先来比较一下Struts2和Spring中AO ...

  8. 引用 模块编译Makefile模板

    本文转载自geyingzhen<模块编译Makefile模板>   引用 geyingzhen 的 模块编译Makefile模板 ifneq ($(KERNELRELEASE), ) // ...

  9. php利用iframe实现无刷新文件上传功能

    上传原理很简单就是利用表单的打开方式为iframe的name名,这样就可以在当前页面的iframe打来了,实现文件上传,再利用js返回上传结果. form target .在 action 属性中规定 ...

  10. 设置不输入密码ssh登录

    在/etc/hosts文件下加入: 192.168.1.60 u60 #设置u60为主机名 在每个节点上创建RSA秘钥: # ssh-keygen -t rsa # 一直按确定键即可 # touch ...