题目大意:
给你一个字符串,问这个字符串最少有多少个回文串。
区间DP直接搞
 
 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+;
const int MAXN = ;
int dp[MAXN][MAXN];
bool P[MAXN][MAXN];
char str[MAXN];
int DFS(int L,int R)
{
if(dp[L][R] != -)
return dp[L][R];
if(L >= R)
return dp[L][R] = ; dp[L][R] = INF;
if(P[L][R]) dp[L][R] = ;
for(int i=L; i<=R; i++)
{
if(P[L][i])
dp[L][R] = min(dp[L][R], + DFS(i+,R));
}
return dp[L][R];
} bool ok(int L,int R)
{
for(int i=L,j=R; i<=j; i++, j--)
{
if(str[i] != str[j])
return false;
}
return true;
} int main()
{
int T, cas = ;
scanf("%d", &T);
while(T --)
{
memset(dp, -, sizeof(dp));
scanf("%s", str);
int len = strlen(str) - ;
for(int i=; i<=len; i++)
for(int j=i; j<=len; j++)
P[i][j] = ok(i, j); printf("Case %d: %d\n",cas ++, DFS(, len) );
} return ;
}

1044 - Palindrome Partitioning(区间DP)的更多相关文章

  1. Lightoj 1044 - Palindrome Partitioning (DP)

    题目链接: Lightoj  1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...

  2. lightoj 1044 - Palindrome Partitioning(需要优化的区间dp)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 题意:求给出的字符串最少能分成多少串回文串. 一般会想到用区间dp暴力3个for ...

  3. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

  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. Cheapest Palindrome(区间DP)

    个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...

  6. Leetcode_1278. Palindrome Partitioning III_[DP]

    题目链接 You are given a string s containing lowercase letters and an integer k. You need to : First, ch ...

  7. Leetcode_132. Palindrome Partitioning II_[DP]

    题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  8. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  9. HDU 4632 Palindrome subsequence (区间DP)

    题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...

随机推荐

  1. 安卓Intent(隐式)

    相对于显式Intent(明确指出了目标组件名称的Intent),隐式Intent,没有明确指出目标组件名称.显式Intent直接用组件的名称定义目标组件,这种方式很直接.但是由于开发人员往往并不清楚别 ...

  2. PHP如何解决网站大流量与高并发的…

    首先,确认服务器硬件是否足够支持当前的流量. 普通的P4服务器一般最多能支持每天10万独立IP,如果访问量比这个还要大, 那么必须首先配置一台更高性能的专用服务器才能解决问题 ,否则怎么优化都不可能彻 ...

  3. couchbase failover 集群故障自动转移方案研究!

    最近迷上Couchbase了,现在所有的站点全部试用Couchbase进行缓存及持久化,这样以来貌似风险比较大啊,缓存服务器挂了就完了. 看到有讲到Couchbase的集群方案很简单,于是照着教程做了 ...

  4. 关于ASP.Net中路径的问题

    比如你的工程是Webapplication1(url是:http://localhost/webapplication1/webform1.aspx) Request.ApplicationPath ...

  5. ca-bundle.crt to java truststore(e.g. trustStore.jks)

    1. download java keyutilhttps://java-keyutil.googlecode.com/files/keyutil-0.4.0.jar 2. run the follo ...

  6. (五)Struts2 标签

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 标签简介 Struts2 自己封装了一套标签,比JSTL ...

  7. JavaScript 标准对象

    在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...

  8. 生产者与消费者(三)---BlockingQueue

    前面阐述了实现生产者与消费者问题的两种方式:wait() / notify()方法 和 await() / signal()方法,本文继续阐述多线程的经典问题---生产者与消费者的第三种方式:Bloc ...

  9. OC中常用的字符处理

    NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...

  10. Linux 特殊权限位

    特殊权限位 LINUX 基本权限有9位但是还有三位特殊权限. suid s(有x权限) S(没有x权限) 4 在用户权限的第三位 sgid s(有x权限) S(没有x权限) 2 在用户组权限的第三位 ...