Codeforces 56D Changing a String (DP)
题意:你可以对字符串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)的更多相关文章
- Codeforces 56D Changing a String 编辑距离 记忆dp
主题链接:点击打开链接 编辑距离.,== 一边dp虽然录制前体累,,依然是dp #include<iostream> #include<cstdio> #include< ...
- Codeforces 56D Changing a String
http://codeforces.com/contest/56/problem/D 题目大意: 一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数. 思路:dp[i][j]代表 ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- 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 ...
- 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 ...
- Codeforces 710 E. Generate a String (dp)
题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. d ...
- codeforces 710E Generate a String(简单dp)
传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...
随机推荐
- HDU 2203 kmp
http://acm.hdu.edu.cn/showproblem.php?pid=2203 亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- ionic2——安装并配置android sdk
下载 android开发者官网下载sdk比较慢,甚至访问不了.所以建议去android中文网下载sdk,如下图找到android-sdk点击链接下载就行了 下载sdk 安装 安装前先要安装java j ...
- New Concept English three (48)
23w/m 76errors In this much-travelled world, there are still thousands of places which are inaccessi ...
- Django中ORM增删改查
新建模型 class Author(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max ...
- C# WebApi 配置复杂路由不生效的问题
配置复杂路由不生效是由于优先级的关系,应该把默认路由放在最后. config.Routes.MapHttpRoute( name: "DynamicApi", routeTempl ...
- docker下安装 Oracle11gR2
这是第二次安装,在第一次安装过程部分内容参考自如下: http://blog.sina.com.cn/s/blog_d840ff330102v4j0.html docker下oracle11g安装 h ...
- UGUI技巧
http://www.cnblogs.com/suoluo/p/5427514.html Text中的可以单独指定某些文字的颜色,只需将想要变色的文本放在<color=**></co ...
- python数据类型,int,str,bool
一,python中的int() int在python中主要用来运算,对字符串的转化,用int(str)表示,并且需要str.isdigit为真. 在int()中二进制的转换如下: #bit_lengt ...
- 有关UCOS_II在LPC1768上的应用
https://www.cnblogs.com/chungshu/archive/2012/12/14/2818380.html
- [转] linux 启动文件及设置环境变量
系统级启动文件 ==================================== 1./etc/rc 主启动文件,不要修改它 2./etc/rc.conf 决定启动哪些系统自带的守护进程 ...