Description

A palindrome partition is the partitioning of a string such that each separate substring is a palindrome.

For example, the string "ABACABA" could be partitioned in several different ways, such as {"A","B","A","C","A","B","A"}, {"A","BACAB","A"}, {"ABA","C","ABA"}, or {"ABACABA"}, among others.

You are given a string s. Return the minimum possible number of substrings in a palindrome partition of s.

Input

Input starts with an integer T (≤ 40), denoting the number of test cases.

Each case begins with a non-empty string s of uppercase letters with length no more than 1000.

Output

For each case of input you have to print the case number and the desired result.

Sample Input

3

AAAA

ABCDEFGH

QWERTYTREWQWERT

Sample Output

Case 1: 1

Case 2: 8

Case 3: 5

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define N 2010000
char s[N];
int f[N],cnt=,t;
bool pdhw(int n,int m){
for(int i=n,j=m;i<=(n+m)/;i++,j--)
if(s[i]!=s[j]) return ; return ;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;i++){
f[i]=i+;
for(int j=;j<=i;j++)
if(pdhw(j,i))
f[i]=min(f[i],f[j-]+);
}
printf("Case %d: %d\n",++cnt,f[len-]);
}
return ;
}

D - Palindrome Partitioning (DP)的更多相关文章

  1. Lightoj 1044 - Palindrome Partitioning (DP)

    题目链接: Lightoj  1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...

  2. Atcoder Yet Another Palindrome Partitioning(状压dp)

    Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...

  3. 132. Palindrome Partitioning II (String; DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. 131. Palindrome Partitioning (Back-Track, DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. Leetcode_1278. Palindrome Partitioning III_[DP]

    题目链接 You are given a string s containing lowercase letters and an integer k. You need to : First, ch ...

  6. Leetcode_132. Palindrome Partitioning II_[DP]

    题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  7. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  8. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

随机推荐

  1. Android 身份证号码查询、手机号码查询、天气查询

    1.基本信息 身份证号码查询:http://apistore.baidu.com/apiworks/servicedetail/113.html 手机号码:http://apistore.baidu. ...

  2. android XMl 解析神奇xstream 六: 把集合list 转化为 XML文档

    前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...

  3. FileInputStream类

    FileInputStream和FileOutPutStream类都是用来操作磁盘文件的.如果用户对文件读取需求比较简单,则可以使用FileInputStream类,该类继承InputStream类 ...

  4. CAS实现单点登入(sso)经典教程

    本教程我已按照步骤实现,不过要深入了解单点登入还需要进一步的学习,掌握其中的精髓. 一.简介 1.cas是有耶鲁大学研发的单点登录服务器 2.本教材所用环境 Tomcat7.2 JDK6 CAS Se ...

  5. .NET下金额大小写转换

    说明:金额转换可以转换50位的数值,单位从分到级.对于中间部分是否显示零,可以根据修改TranslateJInen()函数修改.中间数值为零的去掉不显示 public string GetChCapi ...

  6. 朝花夕拾-android 获取当前手机的内存卡状态和网络连接状态

    序言: 人的一生是一个选择的过程. 如果脚下只有一条路,只要一往无前即可,不用担心走错.即使是错也别无它法.然而人是不安分的,况且安于独木桥的行走,其目的地由于没有蜿蜒曲折去遮挡行路人的视线,一往无前 ...

  7. iOS之UI--通讯录的实例关键知识技术点积累

    通讯录的实例关键知识技术点积累 效果展示: 作为博文笔记,既然是笔记,目的是为了能够以后做这个项目能够快速上手,如果这是我下一次阅览这个博文笔记,那么我应该先空手从零开始做,需求也就是这个项目的展示效 ...

  8. 修复ORACLETNS-12545 因目标主机或对象不存在错误

    现象: ORACLE启动不了,输入cmd->lsnrctl后,出现如下错误, 经查资料,发现是主机名可能解析有问题,后来在D:\oracle\ora92\network\admin下打开list ...

  9. 如何在linux系统中设置静态ip地址

    在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 开始编辑,填写ip地址.子网掩码.网关.DNS等.其中"红框内的信息"是必须 ...

  10. Java异常信息处理

    import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.jun ...