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
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; }
相关文章推荐
- Spine之二——认识界面
- smali语法
- sublime text的部分常用配置
- javadoc生成出现错误“编码 GBK 的不可映射字符
- 百度地图-异步加载地图
- bzoj 1299: [LLH邀请赛]巧克力棒(nim游戏的应用)
- RHEL6.2下挂载光驱安装软件
- leetcode--003. Longest Substring Without Repeating Characters
- 在Ubuntu系统中安装Visual Studio Code
- Spark Streaming揭秘 Day5 初步贯通源码
- spine之一——术语和概念
- 常用的正则表达式-匹配中英文、字母和数字
- NJUST 1920 偷吃糖果【水】
- JDBC基础语法
- page_address_init 函数
- Python Set集合,函数,深入拷贝,浅入拷贝,文件处理--小白基础
- 【VS开发】设备控制台 (DevCon.exe) 示例
- 【VS开发】设备控制台 (DevCon.exe) 命令
- Python:Python基础(一)
- 第七届蓝桥杯省赛7:剪邮票