UVA - 1625 Color Length[序列DP 代价计算技巧]
| UVA - 1625 |
白书
if(bg[c][0]==i&&bg[c][1]>j) w[i][j]++;
if(ed[c][0]==i&&ed[c][1]<=j) w[i][j]--;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int N=,INF=1e9;
int T,n,m;
char a[N],b[N];
int f[N][N],w[N][N],bg[][],ed[][];
void dp(){
memset(bg,,sizeof(bg));
memset(ed,,sizeof(ed));
for(int i=;i<;i++) bg[i][]=bg[i][]=INF;
for(int i=;i<=n;i++){
int c=a[i]-'A';
if(bg[c][]==INF) bg[c][]=i;
ed[c][]=i;
}
for(int i=;i<=m;i++){
int c=b[i]-'A';
if(bg[c][]==INF) bg[c][]=i;
ed[c][]=i;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
//f[i][j]=min(f[i-1][j]+cal(i-1,j),f[i][j-1]+cal(i,j-1));
//f[i][j]=min(f[i-1][j],f[i][j-1])+cal(i,j);
if(i==&&j==) continue;
int t1=INF,t2=INF;
if(i>) t1=f[i-][j]+w[i-][j];
if(j>) t2=f[i][j-]+w[i][j-];
f[i][j]=min(t1,t2); if(i>){
w[i][j]=w[i-][j];
int c=a[i]-'A';
if(bg[c][]==i&&bg[c][]>j) w[i][j]++;
if(ed[c][]==i&&ed[c][]<=j) w[i][j]--;
}else{
w[i][j]=w[i][j-];
int c=b[j]-'A';
if(bg[c][]==j&&bg[c][]>i) w[i][j]++;
if(ed[c][]==j&&ed[c][]<=i) w[i][j]--;
//if(b[j]=='R') printf("R %d %d %d %d\n",bg[c][1],ed[c][1],i,j);
}
//printf("%d %d %d %d\n",i,j,f[i][j],w[i][j]);
}
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%s%s",a+,b+);
n=strlen(a+);m=strlen(b+);
dp();
printf("%d\n",f[n][m]);
}
}
白书的标程用了滚动数组,可以借鉴一下
// UVa1625 Color Length
// Rujia Liu
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ;
const int INF = ; char p[maxn], q[maxn]; // starts from position 1
int sp[], sq[], ep[], eq[]; // sp[i] start positions of character i in p
int d[][maxn], c[][maxn]; // 滚动数组 c[i][j]: how many "incomplete" colors in the mixed sequence int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s%s", p+, q+); int n = strlen(p+);
int m = strlen(q+);
for(int i = ; i <= n; i++) p[i] -= 'A';
for(int i = ; i <= m; i++) q[i] -= 'A'; // calculate s and e
for(int i = ; i < ; i++)
{
sp[i] = sq[i] = INF;
ep[i] = eq[i] = ;
}
for(int i = ; i <= n; i++)
{
sp[p[i]] = min(sp[p[i]], i);
ep[p[i]] = i;
}
for(int i = ; i <= m; i++)
{
sq[q[i]] = min(sq[q[i]], i);
eq[q[i]] = i;
} // dp
int t = ;
memset(c, , sizeof(c));
memset(d, , sizeof(d));
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
if(!i && !j) continue; // calculate d
int v1 = INF, v2 = INF;
//计算d[i][j], d[i][j]由d[i-1][j]或d[i][j-1]添加一个字母得到
if(i) v1 = d[t^][j] + c[t^][j]; // remove from p
if(j) v2 = d[t][j - ] + c[t][j - ]; // remove from q
d[t][j] = min(v1, v2); // calculate c
if(i)
{
c[t][j] = c[t^][j];
if(sp[p[i]] == i && sq[p[i]] > j) c[t][j]++; //出现新的字母
if(ep[p[i]] == i && eq[p[i]] <= j) c[t][j]--; //一个字母已经结束
}
else if(j)
{
c[t][j] = c[t][j - ];
if(sq[q[j]] == j && sp[q[j]] > i) c[t][j]++;
if(eq[q[j]] == j && ep[q[j]] <= i) c[t][j]--;
}
}
t ^= ;
}
printf("%d\n", d[t^][m]);
}
return ;
}
UVA - 1625 Color Length[序列DP 代价计算技巧]的更多相关文章
- UVA - 1625 Color Length[序列DP 提前计算代价]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVa 1625 - Color Length(线性DP + 滚动数组)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1625 Color Length (DP)
题意:给定两个序列,让你组成一个新的序列,让两个相同字符的位置最大差之和最小.组成方式只能从一个序列前部拿出一个字符放到新序列中. 析:这个题状态表示和转移很容易想到,主要是在处理上面,dp[i][j ...
- UVA 1625 Color Length 颜色的长度 (预处理+dp)
dp[i][j]表示前一个序列拿了i个颜色,后一个序列拿了j个颜色的最小花费. 转移的时候显然只能向dp[i+1][j],或dp[i][j+1]转移,每增加拿走一个颜色,之前已经出现但没结束的颜色个数 ...
- UVA 1625 "Color Length" (基础DP)
传送门 •参考资料 [1]:HopeForBetter •题意 •题解(by 紫书) •我的理解 用了一上午的时间,参考紫书+上述博文,终于解决了疑惑: 定义第一个颜色序列用串 s 表示,第二个用串 ...
- UVa 1625 Color Length
思路还算明白,不过要落实到代码上还真敲不出来. 题意: 有两个由大写字母组成的颜色序列,将它们合并成一个序列:每次可以把其中一个序列开头的颜色放到新序列的尾部. 对于每种颜色,其跨度定义为合并后的序列 ...
- 1625 - Color Length——[动态规划]
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 动态规划(模型转换):uvaoj 1625 Color Length
[PDF Link]题目点这里 这道题一眼就是动态规划,然而貌似并不好做. 如果不转换模型,状态是难以处理的. 巧妙地转化:不直接求一种字母头尾距离,而是拆开放到状态中. #include <i ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- 【NopCommerce源码架构学习-二】单例模式实现代码分析
单例模式是是常用经典十几种设计模式中最简单的..NET中单例模式的实现也有很多种方式.下面我来介绍一下NopCommerce中单例模式实现. 我之前的文章就分析了一下nop中EngineContext ...
- C#中实现excel文件批量导入access数据表中
一 .界面简单设计如下: 二 .代码如下: using System; using System.Collections.Generic; using System.ComponentModel; u ...
- SSH输入错误Action
在类型转化.输入验证校验 .文件上传等出错的时候,如Action中某个变量是int,而上传的值是"ABC",此时Action不会执行execute()函数,而是直接返回result ...
- http cancelled状态与ajax 超时
在最近一周,我们的前端在测试某些批量超过的某个步骤时,发现请求好像发出来,但是后台状态没有变化,说是最近才出现的问题,以前一直都是正常的,两天连续出现两次之后,来找笔者,首先检查了下中间件的日志,发现 ...
- 第三十章 elk(1) - 第一种架构(最简架构)
软件版本: es:2.4.0 logstash:2.4.0 kibana:4.6.1 一.logstash安装(收集.过滤日志.构建索引) 1.下载:https://www.elastic.co/do ...
- Android的Message Pool是什么——源码角度分析
原文地址: http://blog.csdn.net/xplee0576/article/details/46875555 Android中,我们在线程之间通信传递通常采用Android的消息机制,而 ...
- 【读书笔记】100个Switf必备tips
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.Selector 在Swi ...
- TextInputLayout setError() setErrorEnable()
public void afterTextChanged(Editable s) { if (textInputLayout.getEditText().getText().toString().le ...
- [C#6] 4-string 插值
0. 目录 C#6 新增特性目录 1. 老版本的代码 internal class Person { public string Name { get; set; } public int Age { ...
- MySQL学习笔记之数据类型
数据类型在所有的数据库使用当中,都是避免不了的部分.以前每次写SQL语句,对于定义成哪种数据类型总是迷迷糊糊,今天就来彻底弄清.以下介绍仅针对MySQL 5.5以上版本. 一.字符串类型 1.char ...