题意:你可以对字符串s进行3种操作:

1,在pos位置插入字符ch。

2,删除pos位置的字符。

3,替换pos位置的字符为ch。

问最少需要多少次操作可以把字符s变成字符s1?

思路:

设dp[i][j]为字符串s的前i个字符替换成s1的前j个字符的最小花费。则有三种转移:

1:dp[i - 1][j - 1] + (s[i] != s1[j]) -> dp[i][j] //将s[i]替换为s1[j] (真实位置是j)。

2:dp[i - 1][j] + 1 ->dp[i][j] //删除i位置的数(对应真实的s的位置是j + 1, 因为这个转移相当于s的前i - 1字符已经变成s1的前j个字符,所以删除的是第j + 1个位置的字符)。

3:dp[i][j - 1] + 1 -> dp[i][j] //在i位置后面添加一个数(真实位置是j)。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1010;
char s[maxn], s1[maxn];
int dp[maxn][maxn];
void print(int i, int j, int remain) {
if(!remain) return;
if(i > 0 && j > 0 && dp[i - 1][j - 1] + (int)(s[i] != s1[j]) == dp[i][j]) {
print(i - 1, j - 1, remain - (int)(s[i] != s1[j]));
if(s[i] != s1[j]) printf("REPLACE %d %c\n", j, s1[j]);
} else if(i > 0 && dp[i - 1][j] + 1 == dp[i][j]) {
print(i - 1, j, remain - 1);
printf("DELETE %d\n", j + 1);
} else if(j > 0 && dp[i][j - 1] + 1 == dp[i][j]) {
print(i, j - 1, remain - 1);
printf("INSERT %d %c\n", j, s1[j]);//j位置插入s1[j]
}
}
int main() {
scanf("%s", s + 1);
scanf("%s", s1 + 1);
int n = strlen(s + 1), m = strlen(s1 + 1);
memset(dp, 0x3f, sizeof(dp));
for (int i = 1; i <= n; i++) dp[i][0] = i;
for (int i = 1; i <= m; i++) dp[0][i] = i;
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (int)(s[i] != s1[j]));
dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + 1, dp[i][j - 1] + 1));
}
printf("%d\n", dp[n][m]);
print(n, m, dp[n][m]);
}

  

Codeforces 56D Changing a String (DP)的更多相关文章

  1. Codeforces 56D Changing a String 编辑距离 记忆dp

    主题链接:点击打开链接 编辑距离.,== 一边dp虽然录制前体累,,依然是dp #include<iostream> #include<cstdio> #include< ...

  2. Codeforces 56D Changing a String

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

  3. CodeForces 710E Generate a String (DP)

    题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...

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

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

  5. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  6. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  7. codeforces 710E E. Generate a String(dp)

    题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...

  8. Codeforces 710 E. Generate a String (dp)

    题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. d ...

  9. codeforces 710E Generate a String(简单dp)

    传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...

随机推荐

  1. Amazon EMR(Elastic MapReduce):亚马逊Hadoop托管服务运行架构&Hadoop云服务之战:微软vs.亚马逊

    http://s3tools.org/s3cmd Amazon Elastic MapReduce (Amazon EMR)简介 Amazon Elastic MapReduce (Amazon EM ...

  2. Technocup 2019 C. Compress String

    一个字符串 $s$,你要把它分成若干段,有两种合法的段 1.段长为 $1$,代价为 $a$ 2.这个段是前面所有段拼起来组成的字符串的字串,代价为 $b$ 问最小代价 $|s| \leq 5000$ ...

  3. [HihoCoder1413]Rikka with String

    vjudge 题意 给你一个串,问你把每个位置的字符替换成#后串中有多少本质不同的子串. \(n\le 3*10^5\) sol 首先可以计算出原串里面有多少本质不同的子串.显然就是\(\sum_{i ...

  4. Bootstrap确定样式让屏幕缩小后布局不乱

    解决方案是如下 结果如下:

  5. python函数-filter()

    filter(func, seq)  filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断, ...

  6. /etc删了怎么办

    实施一个哥们一个手抖,把/etc删掉了:别人无法ssh到上面,除了他.怎么办? 从类似的OK机器中打包一个etc.tar,然后将etc.tar放到OK机器www服务器目录里面:然后在问题机器上面通过w ...

  7. BZOJ3809:Gty的二逼妹子序列

    浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...

  8. jquery-post 异常

    报错:Client closed connection before receiving entire response 前端请求: $.ajax({ type : 'POST' , url : '/ ...

  9. USB CDC & 可变形参

    控制台的三种连接方式: 1.IP网络 2.USB 3.UART 一:介绍USB CDC方式: 1.控制台配置如下: 2.USB Product ID 可以是:0x0000/0x5300/0x0238 ...

  10. Windows Server 2008 修改系统的SID

    故事背景:用VMware搭建了几个操作系统相同的虚拟机.安装成功一台后,直接拷贝已经生成的VMDK文件来构建其它的虚拟机. 一般情况下,如果复制的各个虚拟机只是单独使用,并且这些虚拟机不加入到域(Ac ...