题意:给定两个序列,让你组成一个新的序列,让两个相同字符的位置最大差之和最小。组成方式只能从一个序列前部拿出一个字符放到新序列中。

析:这个题状态表示和转移很容易想到,主要是在处理上面,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)的更多相关文章

  1. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  2. UVA - 1625 Color Length[序列DP 提前计算代价]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  3. UVa 1625 - Color Length(线性DP + 滚动数组)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVA 1625 Color Length 颜色的长度 (预处理+dp)

    dp[i][j]表示前一个序列拿了i个颜色,后一个序列拿了j个颜色的最小花费. 转移的时候显然只能向dp[i+1][j],或dp[i][j+1]转移,每增加拿走一个颜色,之前已经出现但没结束的颜色个数 ...

  5. UVA 1625 "Color Length" (基础DP)

    传送门 •参考资料 [1]:HopeForBetter •题意 •题解(by 紫书) •我的理解 用了一上午的时间,参考紫书+上述博文,终于解决了疑惑: 定义第一个颜色序列用串 s 表示,第二个用串 ...

  6. UVa 1625 Color Length

    思路还算明白,不过要落实到代码上还真敲不出来. 题意: 有两个由大写字母组成的颜色序列,将它们合并成一个序列:每次可以把其中一个序列开头的颜色放到新序列的尾部. 对于每种颜色,其跨度定义为合并后的序列 ...

  7. 动态规划(模型转换):uvaoj 1625 Color Length

    [PDF Link]题目点这里 这道题一眼就是动态规划,然而貌似并不好做. 如果不转换模型,状态是难以处理的. 巧妙地转化:不直接求一种字母头尾距离,而是拆开放到状态中. #include <i ...

  8. 1625 - Color Length——[动态规划]

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. UVA-1625-Color Length(DP LCS变形)

    Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的 ...

随机推荐

  1. jQuery ajax submit form 被拦截问题的解决

    一般情况下用js或jquery的submit方法提交form表单是不会被浏览器拦截的,但是发现异步的情况下用js提交form表单就会被浏览器拦截,这样就对功能的实现带来了很多的麻烦.网上看了好多都是同 ...

  2. Jmeter ----关于上传图片接口

    转自:http://www.cnblogs.com/linglingyuese/p/4514808.html 需求 1 2 3 4 5 6 7 8 9 post上传   Request: { &quo ...

  3. java 执行bat批处理文件 并关闭cmd窗口

    java 执行bat批处理文件 并关闭cmd窗口 import java.io.IOException; public class CmdMain { public static void main( ...

  4. RHEL6.2配置从零开始

    RHEL6.2最小化安装并配置,持续更新中... 1.RHEL6.2最小化安装 RHEL6.2默认安装许多用不到的软件,不仅浪费空间.增大系统开销,还会显得凌乱.所以选择最小化安装.  注意:安装步骤 ...

  5. Jenkins+SVN+maven+Tomcat构建自动化集成任务

    Jenkins安装方法详解:https://www.cnblogs.com/lizhe860/p/9901257.html 一.安装maven插件 1.依次进入系统管理→插件管理→可选插件, 找到Ma ...

  6. 模块的分类以及time与date time 模块 radom模块

    1.标准库,或者内置模块,python解释器自带的,比如sys,os模块 2.开源模块,或者叫第三方模块,python就强大在这里. 3.自定义模块. 标准库: 1.时间模块time与datetime ...

  7. Windows下搭建PHP开发环境-WEB服务器

    PHP集成开发环境有很多,如XAMPP.AppServ......只要一键安装就把PHP环境给搭建好了.但这种安装方式不够灵活,软件的自由组合不方便,同时也不利于学习.所以我还是喜欢手工搭建PHP开发 ...

  8. WEB前端必备掌握知识

    1.跨域: 跨域问题是由于javascript语言安全限制中的同源策略造成的.

  9. 配置key认证登陆Ubuntu (上)

    每一个看似轻松的结果背后都有不为人知的辛酸.又是一件小事,结果折腾了一天. 上接配置好SSH 和Samba后,开始了Python编程实践.由于实在不大会用Vim, 所以最后的编程环境实际上在Windo ...

  10. leetcode773

    使用两种语言实现,先贴C++的 class Solution { public: vector<vector<int>> floodFill(vector<vector& ...