Codeforces 67C Sequence of Balls 编辑距离 dp
题目链接:点击打开链接
有一个交换操作比較特殊,所以记录每一个点距离自己近期的那个字符的位置
然后交换就相当于把第一行要交换的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的更多相关文章
- HDU 4323 Magic Number(编辑距离DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Codeforces 56D Changing a String 编辑距离 记忆dp
主题链接:点击打开链接 编辑距离.,== 一边dp虽然录制前体累,,依然是dp #include<iostream> #include<cstdio> #include< ...
- 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 ...
- Codeforces 13C Sequence dp
题目链接:http://codeforces.com/problemset/problem/13/C 题意: 给定n长的序列 每次操作能够给每一个数++或-- 问最少须要几步操作使得序列变为非递减序列 ...
- Codeforces 13C Sequence --DP+离散化
题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...
- Codeforces 467C George and Job(DP)
题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...
随机推荐
- C语言的学习-基础知识点
---BOOL BOOL BOOL a = YES; printf("%d\n", a); a = NO; printf("%d", a); , b = ; B ...
- Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo
标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...
- 将宿主机东西拷贝到dokcer容器中去
1,获取容器名称或者id : docker ps 2,获取整个容器的id,其实键盘tag就可以补全的. docker inspect -f '{{.Id}}' 步骤A获取的名称或者id 3,在主机 ...
- CentOS管理
1.使用yum安装和卸载软件 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 一.yum list|more 列 ...
- C#创建Windows服务的几个注意事项
1.服务安装后的自动启动:服务的StartType即使配置成Automatic,在首次安装成功之后还是要在服务列表中找到并手工启动.此外,可以通过在ProjectInstaller中添加AfterIn ...
- MySql函数应用
-- 当前时间 now(); -- 查询结果串联(逗号) select group_concat(col_name) from table_name;
- hdu 1009 贪心基础题
B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bi ...
- web qq 获取好友列表hash算法
web qq 获取好友列表hash算法 在使用web qq的接口进行好友列表获取的时候,需要post一个参数:hash 在对其js文件进行分析之后,发现计算hash的函数位于: http://0.we ...
- 可以ping通,浏览器打不开网页 - 解决办法
网络故障表现为: 1.电脑显示网络连接正常,DNS配置和hosts配置均正常 2.cmd可以ping通网址,域名 3.所有浏览器无法打开网页,有道云笔记置灰,微信二维码刷新失败 解决办法: 管理员权限 ...
- iOS7适配问题
iOS7适配问题 2013-09-28 08:32:37 我来说两句 作者:冻僵的企鹅 收藏 我要投稿 iOS 7发布了,适配问题来了,开发者都忙起来了. 先记一个iOS7 的 ...