题意:你可以对字符串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. hdu6237 分解质因子

    题意:给一堆石子,每次移动一颗到另一堆,要求最小次数使得,所有石子数gcd>1 题解:枚举所有质因子,然后找次数最小的那一个,统计次数时,我们可以事先记录下每堆石子余质因子 的和,对所有石子取余 ...

  2. python reload(sys)找不到,name 'reload' is not defined

    在操作数据库的时候遇到这个问题,为什么会出现这种原因?查询如下: python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDeco ...

  3. angular复选框式js树形菜单(二)

    删除(过滤)树形结构某一个子节点: function filterTreeData(treeData){ angular.forEach(treeData,function(item){ if (it ...

  4. Python ord()与chr()函数

    chr():十进制或十六进制数(0-255)转成对应的ASCII字符. ord():ASCII字符转成对应的十进制数. 一个小性质:ASCII表中大写字母排在前面小写排在后面,相差32. 比如: or ...

  5. How to install php 7.x on CentOS 7

    Step 1: Setup the Webtatic YUM repo Precompiled PHP 7.x binaries are available for CentOS 7 from the ...

  6. UVA - 1331 Minimax Triangulation (区间dp)(最优三角剖分)

    题目链接 把一个多边形剖分成若干个三角形,使得其中最大的三角形面积最小. 比较经典的一道dp问题 设dp[l][r]为把多边形[l,r]剖分成三角形的最大三角形面积中的最小值,则$dp[l][r]=m ...

  7. 统计数字noip2007

    7909:统计数字 总时间限制:  1000ms 内存限制:  65536kB 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过1000 ...

  8. 在.net中读写config文件的各种方法(自定义config节点)

    http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...

  9. ORACLE删除用户的有的表的方法

    首先我们查询oracle用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments    ...

  10. [转] 如何用BSP树生成游戏地图

    作者:Timothy Hely 当用对象随机填充某个区域如地下城中的房间时,你可能会遇到的问题是太过随机,导致分布疏密不均或混乱.在本教程中,我将告诉大家如何使用二进制空间划分法(游戏邦注:即Bina ...