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
#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)的更多相关文章
- Lightoj 1044 - Palindrome Partitioning (DP)
题目链接: Lightoj 1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...
- Atcoder Yet Another Palindrome Partitioning(状压dp)
Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 131. Palindrome Partitioning (Back-Track, DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode_1278. Palindrome Partitioning III_[DP]
题目链接 You are given a string s containing lowercase letters and an integer k. You need to : First, ch ...
- Leetcode_132. Palindrome Partitioning II_[DP]
题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
随机推荐
- SAP RFC通信模式
在网络技术中,数据通信可以大致划分为两种基本模式:同步通信和异步通信. 其本义是:异步通信时,通信双方时钟允许存在一定误差:同步通信时,双方时钟的允许误差较小.在SAP的系统间的通信过程中,也借用术语 ...
- “连不上 ArcGIS License Manager ”的一点常用诊断方法
在 ArcGIS Desktop 的问题库中,有一类不算做核心技术问题,但却可能会位列“最常见的问题”之一.简言之一句话,”许可服务器连不上怎么办?!“ 下面就来演绎下问题的诊断过程. 本文仅适用于客 ...
- IOS 支付宝-五福简单框架实现-线性滚动(UICollectionView)
猴年支付宝可算是给大家一个很好的惊喜,刺激.大家都在为敬业福而四处奔波.可是到最后也没有几个得到敬业福德,就像我.不知道大家有没有观察,五福界面的滚动是一个很好的设计.在这里,给大家带来简单的滚动实现 ...
- 【原/转】UITableview性能优化总结
UITableView作为ios中使用最频繁的控件之一,其性能优化也是常常要面对的,尤其是当数据量偏大并且设备性能不足时.本文旨在总结tableview的几个性能优化tips,并且随着认识的深入,本文 ...
- 【原】macbook不睡眠的排查与解决
这几天突然发现手上的macbook pro笔记本不能睡眠了,就算合上盖子也是如此.有没有进入睡眠可以观察右下角的呼吸灯,如果呼吸灯常亮则说明有问题.所谓“工欲善其事,必先利其器”,攻城狮想敲更多更好的 ...
- xib命名注意事项--防止被其他控制器意外地 当做默认的 view了
注意: 1.创建的xib如果不是想给指定的控制器做view的话,命名就要注意了! 2.最好是不要命名和控制器名字相关的xib. 如下举例说明一下: - (void)touchesBegan:(NSSe ...
- NSTimer scheduledTimerWithTimeInterval与timerWithTimeInterval、initWithFireDate的区别
英文原文是这样的: A timer object can be registered in only one run loop at a time, although it can be added ...
- MJExtension简介
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- java网络---查找Internet
连接到Internet的设备称为节点,计算机节点称为host. 为了区别每一台连接互联网的计算机,就有了Internet Protocol地址的概念. IPV4 & IPV6 我们以前默认的是 ...
- linux NFS服务器安装与配置 思路
一,nfs服务优缺点 NFS 是Network File System的缩写,即网络文件系统,可以让不同的客户端挂载使用同一个目录,作为共享存储使用,这样可以保证不同的节点客户端数据一致性,在集群架构 ...