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

时间: 2024-08-05 11:13:55

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

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru

Codeforces 13C Sequence --DP+离散化

题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为将前i个数变为以j为结尾的非递减序列的最少操作次数. 则有: dp[i][j] = min(dp[i][j], min(dp[i][k]) + Cost(原来第i个位置上的数转换到j))  (1 <= k <= j) 即前i个数以j结尾的状态可以由前i-1个数以小于等于j的k结尾的状态转移过来,取

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who

CodeForces 21D Traveling Graph 状压dp+欧拉回路

题目链接:点击打开链接 题意: 给定n个点m条边的无向图 求从1点开始经过每条边至少一次最后回到1点的最小路程 显然就是找一条路径可重复的欧拉回路 思路: 首先对于欧拉回路的结论是:所有点的度数都为偶数 因为所有边至少经过一次,那么可以把题意转换成加最少多少条边使得图满足以上结论 而加的边目的是为了把奇度数转成偶度数,先floyd一下得到任意点间加边的最小花费 dp[i]表示状态i下度数都为偶数的最小花费. 状压dp,把i状态下,所有未选择的点中挑2个奇度数的转移即可. #include <cs

poj3356 字符串的最小编辑距离 dp

poj3356 字符串的最小编辑距离  dp AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10895   Accepted: 4188 Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given bel

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are

POJ 3356 AGTC (编辑距离 DP)

Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missing in