题目链接:http://lightoj.com/volume_showproblem.php?problem=1044

dp[i][j]表示i到j直接的最小回文区间个数,直接看代码

 #include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + ;
int dp[N][N], inf = 1e9;
char str[N];
bool judge(int l, int r) {
for(int i = l - , j = r - ; i < j; ++i, --j) {
if(str[i] != str[j])
return false;
}
return true;
}
int dfs(int l, int r) {
if(l == r)
return dp[l][l] = ;
if(l > r)
return ;
if(dp[l][r] < inf)
return dp[l][r];
int ans = inf;
for(int i = r; i >= l; --i) {
if(judge(i, r))
ans = min(ans, dfs(l, i - ) + );
}
return dp[l][r] = ans;
} int main()
{
int t;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%s", str);
int len = strlen(str);
for(int i = ; i <= len; ++i) {
for(int j = ; j <= len; ++j) {
dp[i][j] = inf;
}
}
printf("Case %d: %d\n",ca, dfs(, len));
}
return ;
}

Light oj 1044 - Palindrome Partitioning(区间dp)的更多相关文章

  1. light oj 1422 Halloween Costumes (区间dp)

    题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...

  2. Light OJ 1031 - Easy Game(区间dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...

  3. Light OJ 1033 - Generating Palindromes(区间DP)

    题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串.   ============================================================= ...

  4. Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)

    http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...

  5. Lightoj 1044 - Palindrome Partitioning (DP)

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

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

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

  7. 1044 - Palindrome Partitioning(区间DP)

    题目大意: 给你一个字符串,问这个字符串最少有多少个回文串. 区间DP直接搞     #include<cstdio> #include<cstring> #include&l ...

  8. HDU4632:Palindrome subsequence(区间DP)

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

  9. 131. Palindrome Partitioning (Back-Track, DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. RCP 主题切换

    第一步 编写css文件,放到项目目录下 第二步 添加切换主题扩展点 第三步 设置主题   public  void switchTheme(String themeID) {           Bu ...

  2. Spring Boot 开发系列一 开发环境的一些九九

    从今天开始写这个Spring Boot 开发系列,我是第二周学习JAVA的,公司号称springboot把JAVA的开发提升到填空的能力,本人是NET转JAVA的,想看看这个填空的东西到底有多强.废话 ...

  3. loj6387 「THUPC2018」绿绿与串串 / String

    还是很好做的,大致就是manacher,每个位置为中心的最长回文串要是能抵到最右边就合法,要是能抵到最左边,那这个点的是否合法取决于以这个点为中心的最长回文串的右端点是否合法. #include &l ...

  4. cf965e Short Code

    ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...

  5. Python框架之Django学习笔记(十一)

    话说上次说到数据库的基本访问,而数据库我们主要进行的操作就是CRUD,也即是做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete),俗 ...

  6. 【Best Time to Buy and Sell Stock】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  7. IOS开发学习笔记034-UIScrollView-xib实现分页

    通过xib实现分页功能的封装 1.首先实现xib UIView 的尺寸为300*130,因为准备的图片为600*260. scrollView属性设置如下: 2.新建一个和xib同名的类 2.1 类方 ...

  8. python 删除重复文件 附源代码

    啥也不说了,直接上源码 #! /usr/bin/env python #coding=utf-8 import os import md5 import time def getmd5( filena ...

  9. [oldboy-django][2深入django]Form组件实现生成: select下拉框, checkbox复选框,radio单选框以及如何实现自定义数据格式要求

    1 需求 - 1Form组件如何实现生成选择类标签: select,check, radio - 默认值 - 保留上次输入的值 - 2自定义验证规则 - RegexField - -

  10. 课堂笔记II