题目大意:
给你一个字符串,问这个字符串最少有多少个回文串。
区间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. phpQuery轻松采集网页内容

    原文地址:phpQuery轻松采集网页内容作者:陌上花开 phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息.更有意思的是,它 ...

  2. viewpager在最后一页滑动之后,跳转到主页面

    [TOC] viewpager在最后一页滑动之后,跳转到主页面 思路 主要有是两个监听, 一是addOnPageChangeListener();二是setOnTouchListener(): add ...

  3. linux rman shell

    # make direcory for backset file and scripts file,in my case /backup/db_bak cd   /backup/db_bak mkdi ...

  4. oracle数据库误删恢复方法

    一.如果只是误删部分数据或者某条数据可以通过 1.select * from 误删除的表明 as of timestamp to_Date('恢复年月日  时分秒', '恢复时间格式')       ...

  5. C#中的面向对象编程

    所有的面向对象语言都具有3个基本特征,C#也是不例外的. 封装---把客观事物封装成类,并将类内部的实现隐藏,以保证数据的完整性: 继承---通过继承可以复用父类的对象: 多态---允许将子对象赋值给 ...

  6. HDU3591找零,背包

    题目大概的意思就是:小强用硬币买东西,硬币有N种,面值为Vi,店家有各种硬币都有无限个,而小强只有Ci个(分别对应Vi) 问最小交易硬币数,就是一个有找零的背包问题啦. 我的上一篇博客跟这hdu359 ...

  7. 【HDU1402】【FNT版】A * B Problem Plus

    Problem Description Calculate A * B.   Input Each line will contain two integers A and B. Process to ...

  8. bootstrap轮播组件,大屏幕图片居中效果

    在慕课网学习bootstrap轮播组件的时候,了解到轮播的图片都放在了类名为item下的img中 视频中老师对图片自适应采用给图片img设置width=100%完成,然而这样自适应处理图片在不同屏幕中 ...

  9. php session 自定义的设置测试

    <?php // ini_set('session.save_handler', 'user'); // 注意 set_session_save_handler() 一定要在 session_s ...

  10. React Native:使用 JavaScript 构建原生应用 详细剖析

    数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生的 iOS 应用——就在今天,Beta 版的仓库释出了! 基于 Pho ...