UVa 1625 Color Length (DP)
题意:给定两个序列,让你组成一个新的序列,让两个相同字符的位置最大差之和最小。组成方式只能从一个序列前部拿出一个字符放到新序列中。
析:这个题状态表示和转移很容易想到,主要是在处理上面,dp[i][j] 表示从第一序列中拿了 i 个字符,从第二序列中拿了 j 个字符的最小和是多少,这个要提前预处理每个字符开始出现和最后出现的位置,然后再用一个c数组来记录已经有多少个字符出现,但没有结束。注意要清空。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5000 + 10;
const int maxm = maxn * 100;
const int mod = 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int dp[maxn][maxn], c[maxn][maxn];
int f1[30], f2[30], r1[30], r2[30];
char s1[maxn], s2[maxn];
int a[maxn], b[maxn]; int main(){
int T; cin >> T;
while(T--){
scanf("%s", s1);
scanf("%s", s2);
n = strlen(s1);
m = strlen(s2);
memset(f1, INF, sizeof f1);
memset(f2, INF, sizeof f2);
memset(r1, 0, sizeof r1); //must clear
memset(r2, 0, sizeof r2); //must clear
for(int i = 1; i <= n; ++i){
a[i] = s1[i-1] - 'A';
f1[a[i]] = min(f1[a[i]], i);
r1[a[i]] = i;
} for(int i = 1; i <= m; ++i){
b[i] = s2[i-1] - 'A';
f2[b[i]] = min(f2[b[i]], i);
r2[b[i]] = i;
} for(int i = 0; i <= n; ++i)
for(int j = 0; j <= m; ++j){
if(!i && !j) continue;
int v1 = INF, v2 = INF;
if(i) v1 = dp[i-1][j] + c[i-1][j];
if(j) v2 = dp[i][j-1] + c[i][j-1];
dp[i][j] = min(v1, v2);
if(j){
c[i][j] = c[i][j-1];
if(f2[b[j]] == j && f1[b[j]] > i) ++c[i][j]; // take care of this '>' not '>='
if(r2[b[j]] == j && r1[b[j]] <= i) --c[i][j]; //take care, too
}
else{
c[i][j] = c[i-1][j];
if(f1[a[i]] == i && f2[a[i]] > j) ++c[i][j];
if(r1[a[i]] == i && r2[a[i]] <= j) --c[i][j];
}
}
printf("%d\n", dp[n][m]);
}
return 0;
}
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 提前计算代价]
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]表示前一个序列拿了i个颜色,后一个序列拿了j个颜色的最小花费. 转移的时候显然只能向dp[i+1][j],或dp[i][j+1]转移,每增加拿走一个颜色,之前已经出现但没结束的颜色个数 ...
- UVA 1625 "Color Length" (基础DP)
传送门 •参考资料 [1]:HopeForBetter •题意 •题解(by 紫书) •我的理解 用了一上午的时间,参考紫书+上述博文,终于解决了疑惑: 定义第一个颜色序列用串 s 表示,第二个用串 ...
- UVa 1625 Color Length
思路还算明白,不过要落实到代码上还真敲不出来. 题意: 有两个由大写字母组成的颜色序列,将它们合并成一个序列:每次可以把其中一个序列开头的颜色放到新序列的尾部. 对于每种颜色,其跨度定义为合并后的序列 ...
- 动态规划(模型转换):uvaoj 1625 Color Length
[PDF Link]题目点这里 这道题一眼就是动态规划,然而貌似并不好做. 如果不转换模型,状态是难以处理的. 巧妙地转化:不直接求一种字母头尾距离,而是拆开放到状态中. #include <i ...
- 1625 - Color Length——[动态规划]
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA-1625-Color Length(DP LCS变形)
Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的 ...
随机推荐
- POJ1742:Coins
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:http://poj.org/problem?id=1742 多重背包,每个物 ...
- 【android】Socket简单用法
Socket通常也称做”套接字“,用于描述IP地址和端口,废话不多说,它就是网络通信过程中端点的抽象表示.值得一提的是,Java在包java.net中提供了两个类Socket和ServerSocket ...
- form表单中name和id区别
HTML文本是由HTML命令组成的描述性文本,HTML命令可以说明文字.图形.动画.声音.表格.链接等.HTML的结构包括头部(Head).主体(Body)两大部分,其中头部描述浏览器所需的信息,而主 ...
- MySQL中的各种引擎
数据库中的存储引擎其实是对使用了该引擎的表进行某种设置,数据库中的表设定了什么存储引擎,那么该表在数据存储方式.数据更新方式.数据查询性能以及是否支持索引等方面就会有不同的“效果”.在MySQL数据库 ...
- nextSibling VS nextElementSibling
2. nextSibling vs nextElementSibling { //FF { 在Firefox中,link2的nextSibling并不是link3,因为两者之间有一个换行符. 这被认为 ...
- 【转】使用Jmeter针对ActiveMQ JMS Point To Point压力测试
准备工作 针对JMS类型的Sampler,需要额外的jar包(这里用的是apache ActiveMQ,将下载的AMQ apache-activemq-5.5.0根目录下的activemq-all-5 ...
- 【转】Jmeter做web压力测试时设置持续时间注意点
头一回使用jmeter做web的压力测试,遇到个很莫名其妙的问题,不管我的线程组怎么设置,它就是执行一次就结束了. 设置循环次数为300,不使用调度器--〉执行一次就结束了,循环次数未生效 设置循环次 ...
- 《拳皇98终极之战OL》系统分析
转自:http://www.gameres.com/467959.html 游戏简述 <拳皇98终极之战OL>是由日本SNK官方正版授权,国内著名游戏公司北京掌趣科技与北京玩蟹科技开发,腾 ...
- WARNING: cell0 mapping not found - not syncing cell0
WARNING: cell0 mapping not found - not syncing cell0
- 如何在老惠普电脑上安装windows xp系统
如何在老惠普电脑上安装windows xp系统 前提,老式的紧凑的惠普台式机,装了linux系统,想要装windows xp系统另作他用.但是使用U盘PE怎么也进不了? 解决办法: 1.拆下惠普主机上 ...