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为替换
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的更多相关文章
- Codeforces 56D Changing a String (DP)
题意:你可以对字符串s进行3种操作: 1,在pos位置插入字符ch. 2,删除pos位置的字符. 3,替换pos位置的字符为ch. 问最少需要多少次操作可以把字符s变成字符s1? 思路: 设dp[i] ...
- Codeforces 56D Changing a String
http://codeforces.com/contest/56/problem/D 题目大意: 一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数. 思路:dp[i][j]代表 ...
- 2018.12.12 codeforces 931E. Game with String(概率dp)
传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...
- Codeforces Hello 2018 E题Logical Expression dp+最短路 好题
j题目链接: http://codeforces.com/contest/913/problem/E 题意: 给你x,y,z三个变量,与& 或| 非! 括号() 四种运算符,规定括 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- leetcode 72.编辑距离(dp)
链接:https://leetcode-cn.com/problems/edit-distance/submissions/ 设dp[i][j]表示串s1前i个字符变换成串s2前j个字符所需要的最小操 ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
随机推荐
- sortable.js 华丽丽的排序
首先导入这几个资源 <link href="/css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type ...
- 11427 - Expect the Expected(概率期望)
11427 - Expect the Expected Some mathematical background. This problem asks you to compute the expec ...
- Aqua Data Studio 查询结果中文乱码
菜单条里[文件]-[选项]会弹出个选项对话框 然后选[一般] 在右边的[外观]以下找到[网格结果字体] 将字体类型改成宋体即可了 watermark/2/text/aHR0cDovL2Jsb2cuY ...
- Eclipse快捷键 10个最有用的快捷键(转载)
现在很多开发人员都在用eclipse.用开发工具,就是为了方便,方便你开发你的软件,方便你管理你的工程,而开发工具提供各种功能大部分会有对应的快捷键,下面就列出了eclipse的快捷键. Ecli ...
- 修改字符串 ToCharArray()
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- c++设置输出精度
float类型的精度6-7位,double类型的变量15-16位,但是float占四个字节,double占八个字节, 但能用float类型不要用double因为double占的字节数多,而且运算速度要 ...
- 字符设备驱动4: ioremap
#define GPIO_OFT(x) ((x) - 0x56000000)#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT ...
- 【集训笔记】博弈论相关知识【HDOJ 1850【HDOJ2147
以下资料来自:http://blog.csdn.net/Dinosoft/article/details/6795700 http://qianmacao.blog.163.com/blog/stat ...
- 【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976
改革春风吹满地 Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted ...
- oracle中if/else
oracle中if/else功能的实现的3种写法 1.标准sql规范 一.单个IF 1. if a=... then ......... end if; 2. if a=... then .... ...