链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631

题意:

输入一个由小写字母组成的字符串(长度不超过1000),你的任务是把它划分成尽量少的回文串。
例如,racecar本身就是回文串;fastcar只能分成7个单字母的回文串,aaadbccb最少分成3个回文串:aaa, d, bccb。

分析:

设d[i]为字符0~i划分成的最小回文串的个数,则d[i] = min{d[j] + 1 | s[j+1~i]是回文串}。
可以先用O(n*n)时间预处理s[i..j]是否为回文串。方法是枚举中心,然后不断向左右延伸,直到左右字符不同为止。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int UP = 1000 + 5;
 7 int d[UP]; // d[i]为前i个字符划分成的最小回文串个数
 8 char s[UP];
 9 bool isp[UP][UP]; // isp[i][j]标记s[i..j]是否为回文串
10
11 void init(char* s, int len){
12     memset(isp, false, sizeof(isp));
13     for(int t = 1; t <= len; t++){
14         for(int f = t, b = t; 1 <= f && b <= len; f--, b++){
15             if(s[f] == s[b]) isp[f][b] = true;
16             else break;
17         }
18         for(int f = t, b = t + 1; 1 <= f && b <= len; f--, b++){
19             if(s[f] == s[b]) isp[f][b] = true;
20             else break;
21         }
22     }
23 }
24
25 int main(){
26     int T, len;
27     scanf("%d", &T);
28     while(T--){
29         scanf("%s", s + 1);
30         init(s, len = strlen(s + 1));
31         for(int t = 1; t <= len; t++){
32             d[t] = t;
33             for(int i = 1; i <= t; i++)
34                 if(isp[i][t]) d[t] = min(d[t], d[i-1] + 1);
35         }
36         printf("%d\n", d[len]);
37     }
38     return 0;
39 }

转载于:https://www.cnblogs.com/hkxy125/p/8576295.html

UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)相关推荐

  1. UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)

    题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...

  2. UVA 11584 Partitioning by Palindromes (字符串区间dp)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVA 11584—— Partitioning by Palindromes

    题意:给定一个串,然后问最少可以分割成多少个回文串. 思路:简单dp,dp[i]=min(dp[j]+1,1<j<=i),两次循环扫一遍即可,考察dp的思想. code: #include ...

  4. uva 11584——Partitioning by Palindromes

    题意:给定一个字符串,把该字符串划分为最少的回文串. 思路:dp,到达i点的回文串长度都存起来,那么dp[i]=min(以i为结尾的最短的回文串长度). code: #include <bits ...

  5. UVA 11584 Partitioning by Palindromes 动态规划 入门

    这个题目的大意就是,给你一个字符串,然后让你求出最少的回文数.我开始傻逼了,写了一个o(n^3)的算法,结果老超时.然后略看了别人的题解,才知道有个如此的转移方程. f[i+1]=min(f[j]+1 ...

  6. uva live 4731 Cellular Network 线性dp

    // uva live 4731 // // 状态很好想: // d(i,j)表示前i个网络分为j组所得到的数学期望的最小值 // 转移方程: // d(i,j) = min(d(k,j-1)+cos ...

  7. UVa在线比赛单题汇总-----DP专题

    动态规划基础 例题 LA 3882 UVa 3882 - And Then There Was One 递推------------无力orz UVa 10635 10635 - Prince and ...

  8. UVA11584 划分成回文串 Partitioning by Palindromes(线性DP划分+DP判断回文串)

    整理的算法模板合集: ACM模板 依旧是线性DP 我们使用闫氏DP分析法 总体DP转移的时间复杂度为O(n2)O(n^2)O(n2). 但是这里牵扯到判断 i\tt ii 到 j\tt jj 是否为回 ...

  9. 0x51.动态规划 - 线性DP(习题详解 × 10)

    目录 0x51.动态规划 - 线性DP 0x51.1 LIS问题 Problem A. 登山 (最长下降子序列) Problem B. 友好城市(思维) Problem C. 最大上升子序列和 0x5 ...

  10. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

最新文章

  1. Mysql进阶(4)——基于MHA的MySQL高可用架构
  2. wsasend发送不可靠_架构师总结:kafka 如何保证数据的可靠性和一致性
  3. JavaSE(一)——HelloWorld
  4. 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数
  5. supermap iserver java 6r许可_SuperMap iserver Java 6R 在 linux 上安装和配置 | 学步园
  6. Web前端笔记-使用Webpack调用echarts画图
  7. python stdout stderr 一起输出_Python日志记录在stdout和stderr之间拆分
  8. HeadFirstJava——4_对象的行为
  9. logback自定义日志配置
  10. abaqus与python后处理_abaqus用Python批量后处理教程!如何从abaqus导出python
  11. NetXRay 工具
  12. IM云通信行业步入快车道,融云或将和Twilio一样实现资本上市
  13. 视觉SLAM十四讲学习笔记——第十三讲 实践:设计SLAM系统
  14. Neural Approaches to Conversational AI
  15. 1.7-秩和相关关系
  16. Flink写RocketMQ支持动态UserProperty
  17. freenas作无盘服务器,Freenas11.3 jail创建失败解决办法
  18. element-ui改变树形菜单小箭头
  19. Git 合并时 --no-ff 的作用
  20. 做算法是屠龙,做工程是狩猎,做数据是养猪!

热门文章

  1. TP-LINK 无线路由器桥接步骤
  2. Linux rpm 命令参数详解
  3. redis shell命令
  4. 负载均衡之让nginx跑起来
  5. UI 假死的可能性和处理方法总结
  6. ini配置文件打开模式 r,r+等
  7. 设计模式——建造者模式 1
  8. My third homework
  9. [最小生成树] 继续畅通工程
  10. UVa11809-Floating-Point Numbers