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
Sample Input
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(动态规划基础)的更多相关文章
- UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数
题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...
- UVA-11584:Partitioning by Palindromes(基础DP)
今天带来一个简单的线性结构上的DP,与上次的照明系统(UVA11400)是同一种类型题,便于大家类比.总结.理解,但难度上降低了. We say a sequence of characters is ...
- UVA11584-Partitioning by Palindromes(动态规划基础)
Problem UVA11584-Partitioning by Palindromes Accept: 1326 Submit: 7151Time Limit: 3000 mSec Problem ...
- 【题解】UVA11584 Partitioning by Palindromes
UVA11584 https://www.luogu.org/problemnew/show/UVA11584 暑假开始刷lrj紫/蓝书DP题 这几天做的一道 思路 预处理出所有的回文串是否存在 前提 ...
- UVa11584 - Partitioning by Palindromes(区间DP)
题目大意 给定一个小写字母组成的字符串S,你的任务是划分成尽量少的回文串 题解 方程就是dp[j]=min(dp[i-1]+1)(i<=j,s[i..j]是回文串) 代码: #include&l ...
- UVA-11584 Partitioning by Palindromes (简单线性DP)
题目大意:给一个全是小写字母的字符串,判断最少可分为几个回文子序列.如:“aaadbccb” 最少能分为 “aaa” “d” “bccb” 共三个回文子序列,又如 “aaa” 最少能分为 1 个回文子 ...
- uva11584 Partitioning by Palindromes
题目大意: 给出一个字符串,把他划分成尽量少的回文串,问最少的回文串个数 /* 先预处理所有回文子串 dp[i]表示字符1~i划分成的最小回文串的个数 */ #include<iostream& ...
- 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 ...
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
随机推荐
- 【Java每日一题】20170217
20170216问题解析请点击今日问题下方的“[Java每日一题]20170217”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...
- Debug始于71年前
摘要: 纪念Grace Hopper发现世界上第一个计算机BUG! 1947年9月9日,Grace Hopper的计算科学团队在哈佛的哈弗Mark II电脑运行程序时遇到一个技术故障.她在发生故障的M ...
- JavaScript字符串转换为数字
今天在工作中碰到了一个问题,要将字符串转换为数字,否则函数不能正常工作, 特地研究了下,写了2个函数,供大家参考,代码如下: /** * 将字符串转换为数字 * @param {Object} str ...
- js (jQuery)分组数据
function getobjArr (data) { var result = []; data.HELMET.system = '系统分类' // console.log(data) $.each ...
- es6 语法 (解构赋值)
1.结构赋值 { let a,b,c; [a,b] = [1,2]; console.log(a,b); //1,2} { let a,b,rest; [a,b,...rest] = [1,2,3,4 ...
- JS apply的巧妙用法以及扩展到Object.defineProperty的使用
Math.max 实现得到数组中最大的一项 var array = [1,2,3,4,5]; var max = Math.max.apply(null, array); console.log(ma ...
- 初识HTTP协议web开发
HTTP协议 HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的 ...
- Copy Control settings
Copy Control settings Skip to end of metadata Created by Rajesh Banka, last modified by Jyoti ...
- Mysql sql 功能分类
分类 DDL:数据定义语言,用于定义数据库对象,比如创建表,列,库等 DML:数据操作语言,用于添加.删除.修改数据 DQL:数据查询语言,用于查询(结果集是虚拟表,放在内存中) DCL:数据控制语言 ...
- (网页)websocket后台调用Service层
百度论坛里面有很多好的方法,借鉴. 重点:因为项目是StringBoot所以我用的是下面的方法很好使: Service.... service = (Service....) ContextLoade ...