Codeforces 56D Changing a String
http://codeforces.com/contest/56/problem/D
题目大意:
一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数。
思路:dp[i][j]代表当前串前i个变成目标串前j个的最小花费。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
char a[],b[];
int n,m;
ll f[][];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void dfs(int n,int m){
if (n==&&m==){
return;
}
if (n>=&&f[n][m]==f[n-][m]+){
dfs(n-,m);
printf("DELETE %d\n",m+);
}else
if (m>=&&f[n][m]==f[n][m-]+){
dfs(n,m-);
printf("INSERT %d %c\n",m,b[m]);
}else{
dfs(n-,m-);
if (a[n]!=b[m])
printf("REPLACE %d %c\n",m,b[m]);
}
}
int main(){
scanf("%s",a+);n=strlen(a+);
scanf("%s",b+);m=strlen(b+);
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
f[i][j]=;
f[][]=;
for (int i=;i<=n;i++)
f[i][]=i;
for (int i=;i<=m;i++)
f[][i]=i;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
f[i][j]=std::min(std::min(f[i-][j],f[i][j-])+,f[i-][j-]+(a[i]!=b[j]));
printf("%I64d\n",f[n][m]);
dfs(n,m);
}
Codeforces 56D Changing a String的更多相关文章
- Codeforces 56D Changing a String 编辑距离 记忆dp
主题链接:点击打开链接 编辑距离.,== 一边dp虽然录制前体累,,依然是dp #include<iostream> #include<cstdio> #include< ...
- Codeforces 56D Changing a String (DP)
题意:你可以对字符串s进行3种操作: 1,在pos位置插入字符ch. 2,删除pos位置的字符. 3,替换pos位置的字符为ch. 问最少需要多少次操作可以把字符s变成字符s1? 思路: 设dp[i] ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...
- 【codeforces 797C】Minimal string
[题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...
- Codeforces 1144 E. Median String
原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
随机推荐
- poj 2287 动态规划
用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...
- css属性之transform
定义和用法 transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 实例 旋转 div 元素: <!DOCTYPE html> < ...
- 关于NAT穿透的一些理解
前些天在知乎回答了一个智能家居远程控制方面的问题,感觉自己对无公网IP地址环境下的穿透问题有些了解.昨天同事拿来一个网络摄像头,安装在ADSL路由器上网的环境下,可以远程查看视频,效果还挺不错,问我厂 ...
- 2014-07-24 .NET实现微信公众号的消息回复与自定义菜单
今天是在吾索实习的第12天.我们在这一天中,基本实现了微信公众号的消息回复与自定义菜单的创建. 首先,是实现消息回复,其关键点如下: 读取POST来的数据流:Stream 数据流变量 = HttpCo ...
- Android动画效果
layout_left_in.xml <?xml version="1.0" encoding="utf-8"?> <layoutAnimat ...
- HDU_2022——海选女主角
Problem Description potato老师虽然很喜欢教书,但是迫于生活压力,不得不想办法在业余时间挣点外快以养家糊口.“做什么比较挣钱呢?筛沙子没力气,看大门又不够帅...”potato ...
- RSA实例破解
Description: Decode the message. You intercept the following message, which you know has been encode ...
- 就是一段程序,可以求出N个不等长列表中取N个元素形成的所有组合
def get_result_in_vector(vector, N, tmp, tmp_result): """ :param vector:所有组合的拼接 :para ...
- The Black Tux | IT桔子
The Black Tux | IT桔子 The Black Tux theblacktux.com
- 【KMP】Number Sequence
KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数. 移动位数 = 已匹配的字符数 - 对应的部分匹配 ...