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的更多相关文章

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

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

  2. Codeforces 56D Changing a String (DP)

    题意:你可以对字符串s进行3种操作: 1,在pos位置插入字符ch. 2,删除pos位置的字符. 3,替换pos位置的字符为ch. 问最少需要多少次操作可以把字符s变成字符s1? 思路: 设dp[i] ...

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

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

  4. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

  5. codeforces 632C The Smallest String Concatenation

    The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...

  6. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  7. 【codeforces 797C】Minimal string

    [题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...

  8. Codeforces 1144 E. Median String

    原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...

  9. CodeForces 710E Generate a String (DP)

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

随机推荐

  1. centos6.5+Django+mysql+nginx+uwsgi

    centos6.5+Django+mysql+nginx+uwsgi 1.nginx的安装.这里采用nginx-1.6.0, 建立一个shell脚本然后执行. #!/bin/bash nginx_ve ...

  2. COJ 0500 杨老师的路径规划(MST)最小生成树

    杨老师的路径规划(MST) 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 为满足同学们需求,杨老师在实验楼4层新建了好多个计算 ...

  3. codeforces284 div1 B:概率dp

    蛋疼的期末..好久没有A题了,,惭愧啊 昨晚打起精神准备做cf 结果竟然忘记注册了..拿学长号看了看题,今早起来补了一道dp 题目大意: 有n首歌,你需要边听边猜 对于第 i 首歌 每听一分钟你猜出它 ...

  4. Spring MVC在接收复杂集合参数

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...

  5. <Win32_9>有意思的程序——抓取屏幕

    Win32学了一段时间了,跟着杨老师的脚步,准备学习MFC,因此最近几天在复习C++,于是发现有将近一周没写博文了…… 今天来写一个较为简单.但是比较有意思的东西 不知大家在理发店理发的时候注意到一个 ...

  6. (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2

    转自:http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2 Upda ...

  7. C++ 推断进程是否存在

    [cpp] view plaincopyprint? #include <windows.h> #include "psapi.h" #include"std ...

  8. C语言文件详解

    1.C语言FILE类,在stdio.h头文件中,FILE类是一个结构体:定义如下: 通过typedef定义了 文件类型 的别名: “FILE”,这样以后需要读写文件的时候直接定义FILE就行了.   ...

  9. android启动优化

    ############################################## # power on till android lock screen comes up # # get ...

  10. SUSE 在Intel举行&quot;Rule The Stack&quot;的竞赛中获得 &quot;Openstack安装最高速&quot;奖

    有关"Rule The Stack": https://communities.intel.com/community/itpeernetwork/datastack/blog/2 ...