题目描述

设A和B是两个字符串。我们要用最少的字符操作次数,将字符串A转换为字符串B。这里所说的字符操作共有三种:

1、删除一个字符;

2、插入一个字符;

3、将一个字符改为另一个字符;

!皆为小写字母!

输入输出格式

输入格式:

第一行为字符串A;第二行为字符串B;字符串A和B的长度均小于2000。

输出格式:

只有一个正整数,为最少字符操作次数。

输入输出样例

输入样例#1: 复制

sfdqxbw
gfdgw
输出样例#1: 复制

4

设dp[ i ][ j ]表示a串1~i转换为b串1~j所需的最小cost;
那么转移的时候可以从dp[ i-1 ][ j ] or dp[ i ][ j-1 ] or dp[ i-1 ][ j-1 ]转移到dp[ i ][ j ];
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ char a[3003], b[3003];
int dp[2002][2002];
int main()
{
// ios::sync_with_stdio(0);
rdstr(a); rdstr(b);
int lena = strlen(a);
int lenb = strlen(b);
for (int i = 1; i <= lena; i++)dp[i][0] = i;
for (int j = 1; j <= lenb; j++)dp[0][j] = j;
for (int i = 1; i <= lena; i++) {
for (int j = 1; j <= lenb; j++)dp[i][j] = inf;
}
for (int i = 1; i <= lena; i++) {
for (int j = 1; j <= lenb; j++) {
if (a[i-1] == b[j-1]) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
}
}
}
printf("%d\n", dp[lena][lenb]);
return 0;
}

  


编辑距离 区间dp的更多相关文章

  1. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  2. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  3. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  4. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  5. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  6. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  7. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  8. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. Windows c++面向对象与可视化编程的基础知识

    1.Windows的程序设计语言:Visual C++,Visual Basic ,Visual c#都是“面向对象”的程序设计语言; 2.Windows的程序设计的对象:是Windows的规范部件, ...

  2. 全球数据库-->基金/管理产品-->分类/行业平均-->开放式分类

    SecID 招募书中所定净费率 换手率% 回报日期(每日) 计价货币 回报-本月以来(每日)计价货币 回报-本季以来(每日)计价货币 回报-本年以来(每日)计价货币 回报-1日(每日)计价货币 回报- ...

  3. UX术语详解:任务流,用户流,流程图以及其它全新术语

    以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 用户体验拥有一长串专业的术语和可交付内容.当在线查看UX相关职位描述时,所罗列的这类术语更是 ...

  4. android 的AlertDialog对话框

    private int selectedFruitIndex = 0;  private void showMsg2() {//  Dialog alertDialog = new AlertDial ...

  5. BTrace追踪Hadoop部署

    Hadoop集成BTrace 1.BTrace 1.1BTrace部署 1.下载BTrace工具包,官网地址:https://kenai.com/projects/btrace/downloads/d ...

  6. How do I determine if I'm being run under the debugger?

    #include <assert.h>#include <stdbool.h>#include <sys/types.h>#include <unistd.h ...

  7. 对SIP摘要认证方案的理解

    一.口令认证常见机制 基于口令认证的系统一般有以下几种口令验证方式: 1.客户端以明文形式将用户名密码通过网络发送到服务器,服务器与已经保存在服务端的用户名密码进行比较,一致则通过验证: HTTP基本 ...

  8. Dockerfile文件参数详解

    参考: https://www.jianshu.com/p/e4b31ca37043 https://blog.csdn.net/u010246789/article/details/54139168 ...

  9. C# SQLite 数据库

    数据库 Oracle.Oracle的应用,主要在传统行业的数据化业务中,比如:银行.金融这样的对可用性.健壮性.安全性.实时性要求极高的业务 MS SQL Server.windows生态系统的产品, ...

  10. c# 利用t4模板,自动生成Model类

    我们在用ORM(比如dapper)的时候,很多时候都需要自己写Model层(当然也有很多orm框架自带了这种功能,比如ef),特别是表里字段比较多的时候,一个Model要写半天,而且Model如果用于 ...