We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a list of one or more disjoint non-empty groups of consecutive characters whose concatenation yields the initial sequence.

For example,

(‘race’, ‘car’) is a partition of ‘racecar’ into two groups. Given a sequence of characters, we can always create a partition of these characters such that each group in the partition is a palindrome! Given this observation it is natural to ask: what is the minimum number of groups needed for a given string such that every group is a palindrome? For example:

• ‘racecar’ is already a palindrome, therefore it can be partitioned into one group.

• ‘fastcar’ does not contain any non-trivial palindromes, so it must be partitioned as (‘f’, ‘a’, ‘s’, ‘t’, ‘c’, ‘a’, ‘r’).

• ‘aaadbccb’ can be partitioned as (‘aaa’, ‘d’, ‘bccb’).

Input

Input begins with the number n of test cases.

Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within.

Output

For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.

Sample Input

3

racecar

fastcar

aaadbccb

Sample Output

1

7

3

题目大意:一串字符串中,找出最少组成字符串

解题思路:

用枚举法枚举起点到终点是否是回文串,即判断j-i是否是回文串,如果是单个的字母,则也单独组成一个回文串

程序代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4. #define MAXN 1010
  5. char a[MAXN];
  6. int d[MAXN];
  7. int min(int x,int y)
  8. {
  9. return x<y?x:y;
  10. }
  11. bool level(int l,int r)
  12. {
  13. int m=(l+r)/;
  14. for(int i=l; i<=m; i++)
  15. if(a[i]!=a[r-i+l]) return false;
  16. return true;
  17. }
  18. int main()
  19. {
  20. int n;
  21. scanf("%d",&n);
  22. while(n--)
  23. {
  24. scanf("%s",a+);
  25. int m=strlen(a+);
  26. d[]=;
  27. for(int i=; i<=m+; i++)
  28. d[i]=;
  29. for(int i=; i<=m; i++)
  30. for(int j=; j<=i; j++)
  31. if(level(j,i))
  32. d[i]=min(d[i],d[j-]+);
  33. printf("%d\n",d[m]);
  34. }
  35. return ;
  36. }

动态规划——H 最少回文串的更多相关文章

  1. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  2. Minimum Palindromic Factorization(最少回文串分割)

    Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...

  3. 51Nod - 1154 回文串划分(最少回文串dp)

    回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式.   a|bb|aabaa - 3 个回文串 a|bb|a|aba|a - 5 个回文串 a|b ...

  4. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  5. 【CPLUSOJ】【动态规划】最短回文串

    题目链接 [问题描述] 如果一个字符串正过来读和倒过来读是一样的,那么这个字符串就被称作回文串.例如abcdcba,abcddbca就是回文串,而abcdabcd不是. 你要解决的问题是:对于任意一个 ...

  6. UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数

    题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...

  7. NYOJ 1023 还是回文(DP,花最少费用形成回文串)

    /* 题意:给出一串字符(全部是小写字母),添加或删除一个字符,都会产生一定的花费. 那么,将字符串变成回文串的最小花费是多少呢? 思路:如果一个字符串增加一个字符 x可以形成一个回文串,那么从这个字 ...

  8. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  9. 动态规划——G 回文串

    G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

随机推荐

  1. Lambda表达式转SQL语句类库

    /* 作者:道法自然   * 个人邮件:myyangbin@sina.cn * 2014-10-1 */ Lambda表达式转SQL语句类库源码下载:http://download.csdn.net/ ...

  2. ORA-01653:表空间扩展失败的问题

    以下内容来源于ORA-01653:表空间扩展失败的问题   今天发现,原来设备的数据表空间只有5M,已经满了,上网去找发现要进行扩展空间. 一.脚本修改方式: ----查询表空间使用情况---使用DB ...

  3. vs编译和运行的区

    编译: 是把代码转变成一系列指令(把源代码翻译为计算机能够识别的语言),产生目标代码,并不限于EXE(EXE只是WINDOWS的东西),这样才能装入内存; 运行: 是运行目标代码(运行EXE),就是执 ...

  4. oracle数据表误删恢复

    1.查看回收站中的表: select object_name,original_name,partition_name,type,ts_name,createtime,droptime from re ...

  5. 非注解SpringMVC

    <!-- SpringMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> < ...

  6. Cocos2dx开发(1)——Win8.1下 NDK r10 环境搭建

    内容简要:仅讲述NDK在Windows环境下搭建方法,至于NDK何物一概不属于本文内容,老鸟或已有环境的跳过. 笔者已安装的环境: vs2013企业版.谷歌官网adt 22.3.0(推荐)省得自己ec ...

  7. SQL Server的事务处理与高级查询

    6.高级查询与脚本 6.1子查询 位于SELECT查询中的SELECT查询. 6.11 标量表达式 select id,val,val-(select avg(val) from tbltest) f ...

  8. contos 安装jdk1.8

    JDK安装配置 查看centos系统32位还是64位, 使用命令uname -a;x86是386,586系列的统称,主要是指指令集合.X64才是cpu对64位计算的支持版本. 1. 下载jdk,本例使 ...

  9. Entity Framework 级联删除

    为一对主从表增加级联删除功能 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.E ...

  10. 利用WebRequest类上传文件

    说明:1.WebRequest类是一个抽象类,所以上传类实际使用的是其子类 2.打开Fiddler软件,监视正常网页的文件上传,可以看到http协议的请求和响应信息,简略说明 (第一行:请求说明 PO ...