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. Invalid file system control data detected

    今天在做mkdir操作时报错:Invalid file system control data detected.检查用户和权限没问题,再检查磁盘空间也没问题.最后在网上找到如下信息: [proble ...

  2. asp.net 1.1网站开发配置出现”Visual Studio .NET 无法创建或打开应用程序”解决方法

    可能的解决方案: 1.注册.net framework 1.1 C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis /i 2,如果配置 ...

  3. POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感

    这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ...

  4. linux部署mongodb及基本操作

    原文:http://blog.csdn.net/jinzhencs/article/details/50930877 一.安装部署mongo 1.创建文件夹 /opt/mongodb/single / ...

  5. iOS - UITableViewCell Custom Selection Style Color

    Customize UITextView selection color in UITableView Link : http://derekneely.com/2010/01/uitableview ...

  6. Uncaught SyntaxError : Unexpected token ILLEGAL js传递带空格的参数

    通常在页面中要让某些内容点击后产生点击事件(非页面跳转)都会使用onclick,但是这样不适于需要传递参数的情况,于是写成直接调用JavaScript函数的方式:<a href=javascri ...

  7. hibernate generator class="" id详解

    “assigned”   主键由外部程序负责生成,在   save()   之前指定一个.         “hilo”   通过hi/lo   算法实现的主键生成机制,需要额外的数据库表或字段提供高 ...

  8. 用C/C++扩展你的PHP(转)

    简 介 英文版下载: PHP 5 Power Programming PHP取得成功的一个主要原因之一是她拥有大量的可用扩展.web开发者无论有何种需求,这种需求最有可能在PHP发行包里找到.PHP发 ...

  9. 第三章 Android绘图机制与处理技巧

    1.屏幕尺寸信息 屏幕大小:屏幕对角线长度,单位“寸”:分辨率:手机屏幕像素点个数,例如720x1280分辨率:PPI(Pixels Per Inch):即DPI(Dots Per Inch),它是对 ...

  10. 自定义控件 闪烁效果的TextView

    使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android ...