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动画(三)

    一.LayoutAnimation布局动画 用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果 在res-anim文件下新建一个动画xml文件 <?xml ve ...

  2. 【读书笔记】iOS-读取文本文件

    一,文本文件的内容. 二,工程目录 三,ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  3. 使用greenDAO生成DAO代码

    研究greenDAO有几天了,刚开始看别人的博客基本都把我带到了沟里,讲gradle把简单的问题搞得非常复杂,而且都是抄来抄去,看来看去也就那么几篇,实在看不下去了,只得硬着头皮自己琢磨,好在终于把这 ...

  4. 多线程在iOS开发中的应用

    多线程基本概念 01 进程 进程是指在系统中正在运行的一个应用程序.每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内. 02 线程 2-1 基本概念 1个进程要想执行任务,必须得有线程 ...

  5. linux64位操作系统装32位jdk解决方法

    /opt/tomcat/tomcat7.0/bin/catalina.sh: /usr/local/java/jdk1.7.0_79/bin/java: /lib/ld-linux.so.2: bad ...

  6. Effective Java 05 Avoid creating unnecessary objects

    String s = new String("stringette"); // Don't do this. This will create an object each tim ...

  7. node.js建立简单应用

    1. 建立工程 进入工程目录 cd D:\workspace\project 全局安装express,express作为命令被安装到了系统中 npm install -g express 查看expr ...

  8. 读书笔记——Windows环境下32位汇编语言程序设计(6)使用浮点指令进行64位除法

    罗云彬 典藏版Page192,mark下. 这段代码看不懂,手册上根本没有fdivr不带操作数的指令. .data dqTickCounter1 dq ? dqTickCounter2 dq ? dq ...

  9. 02_嵌套矩形(DAG最长路问题)

    来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题2: 问题描述:有n个矩形,每个矩形可以用两个整数a,b描述,表示它们的长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中的条件 ...

  10. Linux学习之七——乱码的解决方案

    一.乱码的原因 乱码是编码不统一引起的,有下面一些地方需要注意 1. Linux 系统默认支持的语系数据:这与 /etc/sysconfig/i18n 有关:2. 你的终端界面 (bash) 的语系: ...