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. Spring Boot从入门到精通之:二、Spring Boot整合JPA

    springboot-jpa 开发工具 系统: windows10 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_ ...

  2. (一):C++分布式实时应用框架----整体介绍

    C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework) 版权声明:本文版权及所用技术归属smartguys团队所有,对于抄袭,非经 ...

  3. 微信小程序 table 简单测试

    <view class='AutoTable'> <view id='AutoTableItem'> <block wx:for="{{array}}" ...

  4. vuejs-指令详解

    v-if v-if指令可以完全根据表达式的值在DOM中生成或移除一个元素.如果v-if表达式赋值为false,那么对应的元素就会从DOM中移除:否则,对应元素的一个克隆将被重新插入DOM中,代码如下: ...

  5. 一位ML工程师构建深度神经网络的实用技巧

    一位ML工程师构建深度神经网络的实用技巧 https://mp.weixin.qq.com/s/2gKYtona0Z6szsjaj8c9Vg 作者| Matt H/Daniel R 译者| 婉清 编辑 ...

  6. html 获取数据并发送给后端方式

    一.方式一 使用ajax提交 function detailed() { var date = $("#asset_ip").text() $.ajax({ url: " ...

  7. 冒泡排序算法的C++实现

    直接上代码: #include <iostream> using namespace std; void BubbleSort(int arr[],int n){ ) //在本例中,第1次 ...

  8. Java的关键字

    下面列出Java关键字.这些保留字不能用于常量.变量和任标识示字符的名称 没事儿时多背背,对你没有坏处哒! 类别 关键字 说明 访问控制 private 私有的 protected 受保护的 publ ...

  9. (后端)swagger

    Swagger 文档提供了一个方法,使我们可以用指定的 JSON 或者 YAML 摘要来描述你的 API,包括了比如 names.order 等 API 信息. 你可以通过一个文本编辑器来编辑 Swa ...

  10. html之css选择器学习

    相关内容: 什么是css选择器 标签选择器 类选择器 id选择器 并集选择器(分组选择器) 交集选择器 后代选择器 子标签选择器 属性选择器 相邻兄弟选择器 伪类选择器 伪元素选择器(伪对象选择器) ...