您的位置:首页 > 其它

D - Palindrome Partitioning (DP)

2016-05-08 08:36 393 查看
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
;
int f
,cnt=0,t;
bool pdhw(int n,int m){
for(int i=n,j=m;i<=(n+m)/2;i++,j--)
if(s[i]!=s[j]) return 0;

return 1;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++){
f[i]=i+1;
for(int j=0;j<=i;j++)
if(pdhw(j,i))
f[i]=min(f[i],f[j-1]+1);
}
printf("Case %d: %d\n",++cnt,f[len-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: