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 的字符 ...
随机推荐
- AC自动机修正
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #inc ...
- JavaScript方法undefined/null原因探究及闭包简单实现
昨天一个刚写前端不久的同学发消息问这个问题(如下图): HTML代码为(省略部分代码): <head> <script src="test.js">< ...
- 动态sql语句
当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态来构造SQL查询语句,个人觉得用得比较多的地方就是执行搜索查询的SQL语句.对于搜索,可能要根据搜索条件判断来动态执行SQL语句. 在S ...
- iOS- UITableView could not hold the selected row after reload
I like stackoverflow.. Answer : http://stackoverflow.com/questions/6649202/uitableview-doesnt-keep-r ...
- 由fprintf和printf看C语言三种标准流
一.C语言中的三种标准流 1.标准输入流:stdin 2.标准输出流:stdout 3.标准错误输出流:stderr 他们的类型都是File * 二.fprintf于printf的区别 frintf( ...
- Python笔记:使用pywin32处理excel文件
因为前端同事须要批量的对excel文件做特殊处理,删除指定行,故写了此脚本.同一时候配合config.ini方便不熟悉py的同事使用 #!/usr/bin/env python #-*- coding ...
- 内部类之.this&&.new
一..this 我们都知道this是指当前类中的对象本身,但是在内部类中需要指明外部类时,this不再起作用,那应该怎么做呢?下面,让我们看看: public class DotThis { void ...
- XenServer 使用笔记
XenServer 模拟千兆网卡 这两天用 XenServer 安装 VM,其中一台 VM 是用作无盘测试的 Linux Server,不在主流发行版之列,无奈 XenServer 日前对非主流的 L ...
- CSS3动画(360度旋转、旋转放大、放大、移动)
Title div { width: 120px; height: 120px; line-height: 120px; margin: 20px; background-color: #5cb85c ...
- HttpWebRequest多线程抓取17Track的物流信息
公司的一个系统需要去抓17Track的物流信息,贴上代码有需要的朋友可以参考一下↓ //17Track的抓取地址以及开启的线程数量 <add key="url" value= ...