Problem UVA11584-Partitioning by Palindromes

Accept: 1326  Submit: 7151
Time Limit: 3000 mSec

Problem Description

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case consists of two lines. In the first line, a character string of length n (1 ≤ n ≤ 5,000) that is the color information of the cars in one lane before merging is given. In the second line, a character string of length m (1 ≤ m ≤ 5,000) that is the color information of the cars in the other lane is given. Every color is represented as an uppercase letter in English, hence the number of colors is less than or equal to 26.

 Output

Your program is to read from standard input. Print exactly one line for each test case. The line should contain the sum of color lengths after merging the cars in the two lanes optimally as described above. The following shows sample input and output for two test cases.
 

 Sample Input

2
AAABBCY
ABBBCDEEY
GBBY
YRRGB
 

Sample Output

10

12

题解:这个题真是醉了,自己想状态,总有后效性,想了很久还是看了lrj的思路,恍然大悟,还是很有启发性的,局部有后效性,但是整体上没有,不失为以后考虑问题的一个思路,本以为这个题已经没啥说的了,写的时候才发现细节太多了,if 、else写到恶心,还一直有不对的地方,后来在不断调试过程中发现问题主要出现在只出现在一个字符串中的字符上,想出了一个很好的解决方案,就是将第一次出现的地方初始化为INF,没出现就代表着出现在无穷远处,将最后出现的地方初始化为0,没出现就代表着在最前面结束,有了这个初始化就好写很多,调一调就好,dp相对转移起来还是比较简单的,方程见代码。

 #include <bits/stdc++.h>
//#define DE using namespace std; const int maxn = + ;
const int kind = ;
const int INF = 0x3f3f3f3f; char a[maxn], b[maxn];
int num[maxn][maxn], dp[maxn][maxn];
int st[][kind], tail[][kind];
int alen, blen; void pre_management(int p, char *str) {
int len = strlen(str + );
for (int i = ; i <= len; i++) {
if (st[p][str[i]] == INF) st[p][str[i]] = i;
}
for (int i = len; i >= ; i--) {
if (tail[p][str[i]] == ) tail[p][str[i]] = i;
}
} void init() {
memset(st, INF, sizeof(st));
memset(tail, , sizeof(tail));
pre_management(, a); pre_management(, b); int cnt = ;
for (int i = ; i <= alen; i++) {
for (int j = ; j <= blen; j++) {
if (i) {
num[i][j] = num[i - ][j];
if (i == st[][a[i]] && j < st[][a[i]]) num[i][j]++;
if (i == tail[][a[i]] && j >= tail[][a[i]]) num[i][j]--;
}
if (j) {
num[i][j] = num[i][j - ];
if (j == st[][b[j]] && i < st[][b[j]]) num[i][j]++;
if (j == tail[][b[j]] && i >= tail[][b[j]]) num[i][j]--;
}
}
}
} void solve() {
dp[][] = ;
for (int i = ; i <= alen; i++) {
for (int j = ; j <= blen; j++) {
if (!i && !j) continue;
dp[i][j] = INF;
if (i) {
dp[i][j] = min(dp[i - ][j] + num[i][j], dp[i][j]);
}
if (j) {
dp[i][j] = min(dp[i][j - ] + num[i][j], dp[i][j]);
}
}
}
printf("%d\n", dp[alen][blen]);
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%s%s", a + , b + );
alen = strlen(a + ), blen = strlen(b + );
init();
solve();
#ifdef DE
for (int i = ; i <= alen; i++) {
for (int j = ; j <= blen; j++) {
printf("num[%d][%d]: %d ", i, j, num[i][j]);
}
printf("\n");
}
for (int i = ; i <= alen; i++) {
for (int j = ; j <= blen; j++) {
printf("%d ", num[i][j]);
}
printf("\n");
}
#endif // DE
}
return ;
}

UVA11584-Partitioning by Palindromes(动态规划基础)的更多相关文章

  1. UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数

    题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...

  2. UVA-11584:Partitioning by Palindromes(基础DP)

    今天带来一个简单的线性结构上的DP,与上次的照明系统(UVA11400)是同一种类型题,便于大家类比.总结.理解,但难度上降低了. We say a sequence of characters is ...

  3. UVA11584-Partitioning by Palindromes(动态规划基础)

    Problem UVA11584-Partitioning by Palindromes Accept: 1326  Submit: 7151Time Limit: 3000 mSec Problem ...

  4. 【题解】UVA11584 Partitioning by Palindromes

    UVA11584 https://www.luogu.org/problemnew/show/UVA11584 暑假开始刷lrj紫/蓝书DP题 这几天做的一道 思路 预处理出所有的回文串是否存在 前提 ...

  5. UVa11584 - Partitioning by Palindromes(区间DP)

    题目大意 给定一个小写字母组成的字符串S,你的任务是划分成尽量少的回文串 题解 方程就是dp[j]=min(dp[i-1]+1)(i<=j,s[i..j]是回文串) 代码: #include&l ...

  6. UVA-11584 Partitioning by Palindromes (简单线性DP)

    题目大意:给一个全是小写字母的字符串,判断最少可分为几个回文子序列.如:“aaadbccb” 最少能分为 “aaa” “d” “bccb” 共三个回文子序列,又如 “aaa” 最少能分为 1 个回文子 ...

  7. uva11584 Partitioning by Palindromes

    题目大意: 给出一个字符串,把他划分成尽量少的回文串,问最少的回文串个数 /* 先预处理所有回文子串 dp[i]表示字符1~i划分成的最小回文串的个数 */ #include<iostream& ...

  8. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  9. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  10. nyist oj 79 拦截导弹 (动态规划基础题)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...

随机推荐

  1. (7)Jquery1.8.3快速入门_内容过滤选择器

    一.Jquery的内容过滤选择器: 内容过滤选择器: 1.:contains(text) 选取含有文本内容为text的元素 2. :empty 选取不包含子元素或者文本为空的元素 3.:has(sel ...

  2. 开源前端脚本错误监控及跟踪解决项目-BadJS 试用

    BadJS 是 一个web 前端脚本错误监控及跟踪项目.此项目为鹅厂 imweb(qq群:179045421) 团队的开源项目.此项目支持单机,集群,docker.存储支持mongodb等. 官网文档 ...

  3. K8S 调度器,预选策略,优选函数

    Kubernetes Scheduler 提供的调度流程分三步: 预选策略(predicate) 遍历nodelist,选择出符合要求的候选节点,Kubernetes内置了多种预选规则供用户选择. 优 ...

  4. Django的下载安装以及实现一个简单示例

    一.Django下载安装 Django下载链接 1. 下载Django: pip3 install django==1.11.9    (大的版本1.11不要错) 2.创建一个django proje ...

  5. finally知识讲解

    finally语句一定会执行吗,很多人认为一定会,其实未必,只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行.假如在try语句之前执行了return操作 ...

  6. jquery带参插件函数的编写

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  7. tornado 模板引擎

    在tornado的模板引擎中,有两种方式,UImethod与UImodule 自定义方法 在模板中调用方法: tornado:与Django一样使用{{}},但是对于for循环之类,Django以{% ...

  8. socket通讯,TCP,UDP,HTTP的区别

    socket编程有TCP和UDP, TCP:传送控制协议(Transmission Control Protocol) 传输控制协议TCP是TCP/IP协议栈中的传输层协议,它通过序列确认以及包重发机 ...

  9. php post接口,登录功能

    登录功能同注册功能一样,都是使用 post 方法,在执行 sql 语句时,同样要使用 "select * from 表名 where 键名 = 参数" 的查询方式,不同的是: 注册 ...

  10. 《高性能JavaScript》--读书笔记

    第一章 加载和运行 延迟脚本 defer 该属性表明脚本在执行期间不会影响到页面的构造,脚本会先下载但被延迟到整个页面都解析完毕后再运行.只适用于外部脚本 <script src="j ...