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. Java 学习笔记 反射与迭代器

    反射 使用反射获得类 Class cls = Class.forName("全类名") //包括包名 Class cls = xx.Class;//xx代表类名 使用反射获得构造方 ...

  2. Spring Bean的生命周期相关博客

    最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...

  3. hive 导出如数为csv格式

    hive导出csv格式的数据 命令如下: hive -e "select concat_ws(',',appKey,clickJson) adt.jsontest as st from &q ...

  4. 谈谈 final finally finalize 区别

    声明 本篇所涉及的提问,正文的知识点,全都来自于杨晓峰的<Java核心技术36讲>,当然,我并不会全文照搬过来,毕竟这是付费的课程,应该会涉及到侵权之类的问题. 所以,本篇正文中的知识点, ...

  5. vue从入门到进阶:渲染函数 & JSX(八)

    Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更接 ...

  6. jQuery计算文本宽度和input标签根据输入字符动态自适应宽度的实现

    jQuery计算文本宽度的原理是利用html提供的<pre>标签,向dom中动态添加<pre>标签,标签里的内容就是要测试长度的文本,获取完长度之后再删除刚才添加的<pr ...

  7. vue.js及项目实战[笔记]— 03 vue.js插件

    一. vue补充 1. 获取DOM元素 救命稻草,document.querySelector 在template中标示元素`ref = "xxx" 在要获取的时候,this.$r ...

  8. 浅谈servlet与jsp的关系

    servlet是用java语言编写的,是一个java类.主要功能是用来接受.处理客户端的请求,并把处理结果返回到客户端显示.Jsp是servlet发展后期的产物.在没有jsp之前,servlet利用输 ...

  9. Django JsonResponse与HttpResponse重要区别

    JsonResponse是HttpResponse的一个子类,是Django提供的用于创建JSON编码类型响应的快捷类.它的默认Content-Type头部设置为application/json,它的 ...

  10. linux下objdump应用

    <a href="http://www.maomao365.com/?p=952" > linux命令objdump的用法  http://www.maomao365. ...