隐藏页面特效

D - Palindrome Partitioning (DP)

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

1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 #define N 2010000 6 char s[N]; 7 int f[N],cnt=0,t; 8 bool pdhw(int n,int m){ 9 for(int i=n,j=m;i<=(n+m)/2;i++,j--) 10 if(s[i]!=s[j]) return 0; 11 12 return 1; 13 } 14 int main(){ 15 scanf("%d",&t); 16 while(t--){ 17 scanf("%s",s); 18 int len=strlen(s); 19 for(int i=0;i<len;i++){ 20 f[i]=i+1; 21 for(int j=0;j<=i;j++) 22 if(pdhw(j,i)) 23 f[i]=min(f[i],f[j-1]+1); 24 } 25 printf("Case %d: %d\n",++cnt,f[len-1]); 26 } 27 return 0; 28 }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/5469747.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(312)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示