题目链接:点击打开链接

有一个交换操作比較特殊,所以记录每一个点距离自己近期的那个字符的位置

然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉

把第二行要交换的2个字符 之间的字符都插入第一行的2个字符之间

然后再进行交换。

#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
#define inf 10000000
#define N 4005
#define get_min(a,b) {a=min(a,b);}
int dp[N][N], t1, t2, t3, t4;
int p1[N][30], p2[N][30];
char s[N], t[N];
int n, m;
void input(){
scanf("%s",s+1); scanf("%s",t+1);
n = strlen(s+1); m = strlen(t+1);
memset(p1[0], -1, sizeof p1[0]);
memset(p2[0], -1, sizeof p2[0]);
}
int main(){
int i,j,x,y;
while(~scanf("%d %d %d %d",&t1,&t2,&t3,&t4)){
input();
for(i = 0; i <= n; i++)for(j = 0; j <= m; j++)dp[i][j] = inf;
dp[0][0] = 0;
for(i = n; i ; i--)
{
memcpy(p1[i], p1[i+1], sizeof p1[i]);
p1[i][s[i]-'a']=i;
}
for(i = m; i; i--)
{
memcpy(p2[i], p2[i+1], sizeof p2[i]);
p2[i][t[i]-'a']=i;
}
for(i = 0; i <= n; i++)
for(j = 0; j <= m; j++)
{
if(s[i+1] == t[j+1])
get_min(dp[i+1][j+1], dp[i][j]);
get_min(dp[i+1][j], dp[i][j]+t2);
get_min(dp[i][j+1], dp[i][j]+t1);
get_min(dp[i+1][j+1], dp[i][j]+t3);
if ((x = p1[i + 2][t[j + 1] - 'a']) && (y = p2[j + 2][s[i + 1] - 'a'])) {
get_min(dp[x][y], dp[i][j] + (x - i - 2) * t2 + (y - j - 2) * t1 + t4);
}
}
cout<<dp[n][m]<<endl;
}
return 0;
}

Codeforces 67C Sequence of Balls 编辑距离 dp的更多相关文章

  1. HDU 4323 Magic Number(编辑距离DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个 ...

  2. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  3. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  4. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

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

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

  6. codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)

    题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...

  7. Codeforces 13C Sequence dp

    题目链接:http://codeforces.com/problemset/problem/13/C 题意: 给定n长的序列 每次操作能够给每一个数++或-- 问最少须要几步操作使得序列变为非递减序列 ...

  8. Codeforces 13C Sequence --DP+离散化

    题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...

  9. Codeforces 467C George and Job(DP)

    题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...

随机推荐

  1. UTF8转GB2312(UTF8解码)

    小弟C++上手没多久,代码不严谨之处敬请见谅.英语也不是很好,有的是直接使用的拼音. string MyUTF_8toGB2312(string str) { ,,str.c_str(),-,NULL ...

  2. Android-Context的IO功能

    如何将应用数据保存到本地文件?如何从本地文件加载数据到应用中?我实现的步骤是: 应用(java数据)<--org.json-->JSONString<--Context.IO--&g ...

  3. Ztree中simpleData是怎么转换成标准ztree数组对象的

    今天遇到一个自己构造树的情况,树是动态的,预先不知道根节点,用的是easyUI中的tree,于是参考了下Ztree中的实现,恍然大悟,遂记之: transformTozTreeFormat: func ...

  4. js-Array

    1.合并数组 var a = ["aaa", "aaaa"];var b = ["bbb", "bbbb", " ...

  5. 定义block块

    一: 工程图 二: 代码区 AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <U ...

  6. hdu1054 树状dp

    B - 树形dp Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:10000KB     64bit ...

  7. hdu 畅通工程续

    算法:多源最短路(floyd) 题意:有多个城镇,有些之间有通路,给你起点和终点,输出最短路径: Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路 ...

  8. C++ BitArray 引用计数实现

    #ifndef __BITARRAY__ //数组不支持多线程同时访问,没有对引用计数以及分配的资源做任何锁处理 #define __BITARRAY__ 1 //越界访问修改为抛出异常 #ifdef ...

  9. form 表单练习

    创建战网通行证账号,开启您的冒险之旅.<form>    <table border="0">        <tr>            & ...

  10. sort命令总结

    功能:排序 语法:sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--ver ...