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 ...
随机推荐
- Gunplot 命令大全
在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. plot命令 gnuplot> plot sin(x) with line linetype 3 linew ...
- fread(),fwrite() 读/写流
C 库函数 - fread() 描述 C 库函数 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 从给定流 strea ...
- LDAP禁止匿名访问
LDAP默认是允许用户匿名访问的,如下图:在使用工具连接时,勾选匿名绑定后,不需要输入UserDN和密码就可能连接到LDAP服务器,但是只能进行read及search操作.不能做任何的修改及删除操作. ...
- ORA-02069: global_names parameter must be set to TRUE for this operation
原因:在对远程表增删改操作的时候,调用了本地函数. 比如:insert into trans_load_rate@DC values(rate_s(1)); trans_load_rate是DC库的 ...
- 通过JS控制textarea的输入长度
废话不多说,直接上代码(因为自己也只是遇到的时候然后上网查到的解决办法,放在此处只是为了方便各位看以及以后再碰到用起来方便) <ul> <li> <textarea ro ...
- Power Designer - 反向获取数据库物理模型时Unable to list the users 异常
解决方案: 菜单栏 -> Database -> Change Current DBMS ,给DBMS选择Oracle Version 9i2.
- c# 代码执行时间
Stopwatch sw = new Stopwatch(); sw.Start(); Thread.Sleep(2000); sw.Stop(); System.Diagnostics.Trace. ...
- datatable列操作
DataTable myDt =dt; //删除列 myDt.Columns.Remove("minArea"); myDt.Columns.Remove("max ...
- 滚动到指定元素的id处+当元素出现在浏览器显示区域就会自动加载
//滚动到指定元素的id处 如:$("#Exam82") function Jump() { var scroll_offset = $("#Exam82"). ...
- Tempter of the Bone--hdu1010--zoj2110
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...