题目链接

一个长度1000的字符串最少划分为几个回文字符串

-----------------------------------------------------------------------------------------------------------------

想复杂了。

首先N2的时间预处理一下,从i开始长度为len的字符串是否为回文串。

dist(i) = MIN(dist(i),dist(j)+1) 如果 j-i 为一个回文串

  1. #include <set>
  2. #include <map>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <algorithm>
  13.  
  14. #define MAX(a,b) ((a)>=(b)?(a):(b))
  15. #define MIN(a,b) ((a)<=(b)?(a):(b))
  16. #define OO 0x0fffffff
  17. using namespace std;
  18. typedef long long LL;
  19. const int N = ;
  20. bool dp[N][N];
  21. int minDist[N];
  22. char str[N];
  23. int main(){
  24. int n; for(cin>>n;n--;){
  25. scanf("%s",str); int length = strlen(str);
  26.  
  27. for(int i=;i<length;i++) minDist[i] = i+;
  28.  
  29. memset(dp,false,sizeof(dp));
  30. for(int len=;len<;len++) for(int i=;i<length;i++) {
  31. dp[len][i]=true;
  32. }
  33. for(int len=;len<=length;len++){
  34. for(int i=;i<length;i++){
  35. if(len+i>length) break;
  36. dp[len][i] = (str[i]==str[i+len-])&&(dp[len-][i+]);
  37. }
  38. }
  39. for(int i=;i<length;i++){
  40. for(int j=;j<=i;j++){
  41. if(dp[i-j+][j]) minDist[i] = MIN(minDist[i],j?(minDist[j-]+):);
  42. }
  43. }
  44. printf("%d\n",minDist[length-]);
  45. }
  46. return ;
  47. }
  48.  
  49. /*
  50. 3
  51. racecar
  52. fastcar
  53. aaadbccb
  54. */

uva 11584 - 字符串 dp的更多相关文章

  1. Partitioning by Palindromes UVA - 11584 简单dp

    题目:题目链接 思路:预处理出l到r为回文串的子串,然后如果j到i为回文串,dp[i] = min(dp[i], dp[j] + 1) AC代码: #include <iostream> ...

  2. UVA 11584 入门DP

    一开始把它当成暴力来做了,即,从终点开始,枚举其最长的回文串,一旦是最长的,马上就ans++,再计算另外的部分...结果WA了 事实证明就是一个简单DP,算出两个两个点组成的线段是否为回文,再用LCS ...

  3. UVA - 11584 划分字符串的回文串子串; 简单dp

    /** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...

  4. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  5. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  6. 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 ...

  7. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  8. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  9. 【BZOJ 2121】 (字符串DP,区间DP)

    2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...

随机推荐

  1. 三种启动SQLSERVER服务的方法(启动sqlserver服务器,先要启动sqlserver服务)

    1.后台启动 计算机-管理-服务和应用程序 2.SQL SERVER配置管理器 3.在运行窗口中使用命令进行启动:

  2. Sqlite基本命令集合(linux/fedora/ubuntu)

    注:fedora自带sqlite3,无需安装,直接输入命令sqlite3即可. ------------Ubuntu在命令行输入sqlite3,确认没有安装在进行--- 1.安装sqlite3 ubu ...

  3. Linux常见后缀缩写含义

    ctl: control rc: run control (A run-control file is a file of declarations or commands associated wi ...

  4. Unity 动画系统(Mecanim) 术语及翻译 表格

    原文 翻译 Animation Clip 视频片段 Avatar 阿凡达 Retargeting 重定向 Rigging 绑定 skinning 蒙皮 Animator Component 动画组件 ...

  5. vue 中判断向上滚动还是向下滚动

    <script> export default { data(){ return{ i = 0 } }, mounted () { window.addEventListener('scr ...

  6. Pyhton学习——Day29

    #异常与错误# 什么是异常?# 异常就是程序运行时发生错误的信号,在程序出现错误时,则会产生异常,若没有程序处理,则会抛出异常# 导致程序在异常语句处崩溃终止# Traceback 追踪异常信号:** ...

  7. Web API Filter

    在Web Api中,有三种Filter Filter类型 实现的接口 描述 Authorization IAuthorizationFilter 最先运行的Filter,被用作请求权限校验 Actio ...

  8. java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap

    最近在使用java PiO导入导出Excle在windos本机上运行没有问题: 但是!!问题来了!放到Linux服务器上部署后出现异常 java.lang.NoClassDefFoundError: ...

  9. jquery 用于操作动态元素的delegate/on方法

    delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素). 在做项目中有很多由ajax动态生成的html标签,jquery对这些标签不会响应\((selector).c ...

  10. [LeetCode] 350. 两个数组的交集 II intersection-of-two-arrays-ii(排序)

    思路: 先找到set的交集,然后分别计算交集中的每个元素在两个原始数组中出现的最小次数. class Solution(object): def intersect(self, nums1, nums ...