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 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
随机推荐
- python使用tcp实现一个简单的下载器
上一篇中介绍了tcp的流程,本篇通过写一个简单的文件下载器程序来巩固之前学的知识. 文件下载器的流程如下: 客户端: 输入目标服务器的ip和port 输入要下载文件的名称 从服务器下载文件保存到本地 ...
- 异常:Data = 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。
做项目的时候,将DataTable序列化成Json,通过ashx向前台返回数据的时候,前台总是获取不到数据,但是程序运行却没问题, 没抛出异常.一时找不到办法,减小输出的数据量,这时前台可以接收到页面 ...
- Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】
热烈庆祝 Laravel 5.5 LTS 发布! 实际上 Laravel 上一个 LTS 选择 5.1 是非常不明智的,因为 5.2 增加了许许多多优秀的特性.现在好了,大家都用最新的长期支持版本 5 ...
- 2018-08-06 在Office的VBA代码里中文命名
在Excel处理数据时, 顺便试了一下VBA代码编辑器里输入中文, 结果显示为乱码. 查了一下发现VBA本身支持Unicode, 但需要设置系统配置使编辑器能够正常显示, 即设置简体中文为Curren ...
- Linux 安装 tomcat
创建目录 cd /usr mkdir tomcat cd tomcat 上传 tomcat rz.ftp 或者 wget 都可以 解压 tar -xzvf apache-tomcat-8.0.53.t ...
- 配置多个相同网段的ECMP下一跳,配合NQA健康检查实现高可靠性
1.一般情况下,ECMP常用的常见是,针对很远的目的地址,下一跳分别是路由器的不同出端口,而路由器的不同端口是不同网段的,也就是说,下一跳是不同的网段地址. 但是,在连接到终端服务器时,常常会采用多个 ...
- iOS------教你如何APP怎么加急审核
苹果的加急审核如何使用呢? 在iTunesconnect页面,点击右上角的“?”图标,在弹出菜单中选择“联系我们”, 联系我们 然后在Contact Us页面,选择“App Review” —> ...
- python itchat 爬取微信好友信息
原文链接:https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg 「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚 ...
- C# 利用PrintDocument定制打印单据
本文是利用PrintDocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正. 涉及知识点: PrintDocument :从 Windows 窗体应用程序打印时,定义一种可重用 ...
- git 入门教程之配置 git
配置 git 安装完成后,还需要最后一步配置就可以愉快使用了,在命令行输入: git config --global user.name "your username" git c ...