回文串问题。给出一个字符串,问最少可以划分为多少个字符串子串。

对于判断是否为回文串,对于不是很长的字符串,可以采取直接暴力,即从两边向中间收缩判断字符相等。

 bool is_pali(int l, int r)
{
while(l < r)
{
if(str[l] != str[r]) return false;
++l; --r;
}
return true;
}

本题为简化复杂度,可以先预处理str[j...i]是否为回文串。

设dp[i]表示以第i个字符结尾的子串最少可以划分的回文串数。

则dp[i] = min{dp[i], dp[j-1]+1}; (j <= i && str[j...i]是回文串);

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
char str[];
int dp[];
bool is_pali(int l, int r)
{
while(l < r)
{
if(str[l] != str[r]) return false;
++l; --r;
}
return true;
}
int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%s", str+);
int l = strlen(str+);
memset(dp, , sizeof(dp)); //dp[i]:以i结尾的串最少可以划分的回文串数
for(int i = ; i <= l; i++)
{
dp[i] = i; //最多可以划分为i+1个
for(int j = ; j <= i; j++)
{
if(is_pali(j, i))
dp[i] = min(dp[i], dp[j-]+); //此处避免下标越界
}
}
cout << dp[l] << endl;
}
return ;
}

【线性结构上的动态规划】UVa 11584 - Partitioning by Palindromes的更多相关文章

  1. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  2. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  3. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  4. 紫书 例题 9-7 UVa 11584 (线性结构上的动态规划)

    这道题判断回文串的方法非常的秀! 这里用到了记忆化搜索,因为会有很多重复 同时用kase来区分每一组数据 然后还有用递归来判断回文,很简洁 然后这种线性结构的动态规划的题,就是把 当前的这个数组分成两 ...

  5. 【线性结构上的动态规划】UVa 11400 - Lighting System Design

    Problem F Lighting System Design Input: Standard Input Output: Standard Output You are given the tas ...

  6. 紫书 例题 9-6 UVa 11400 (线性结构上的动态规划)

    这道题的下标从1开始比较方便,一方面前缀和算的方便一些,一方面涉及到前j 个灯泡,那么如果从0开始,前3个灯泡就是第0, 1, 2, 3个,非常奇怪. 所以灵活换下标. 然后这道题的动规有点暴力枚举的 ...

  7. UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. DP入门(4)——线性结构上的动态规划

    一.最长上升子序列(LIS) 给定n个整数A1,A2,…,An,按从左到右的顺序选出尽量多的整数,组成一个上升子序列(子序列可以理解为:删除0个或多个数,其他数的顺序不变).例如序列1,6,2,3,7 ...

  9. UVA 11584 Partitioning by Palindromes (字符串区间dp)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. BestCoder Round #70 Jam's math problem(hdu 5615)

    Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ...

  2. redis神器

    redis是内存型数据库,数据保存在内存中,通过tcp直接存取,优势是速度快,并发高,缺点是数据类型有限,查询功能不强,一般用作缓存. redis具有持久化机制,可以定期将内存中的数据持久化到硬盘上. ...

  3. Andoird Studio 错误: 非法字符: '\ufeff' 解决方案。

    从网上下载一个安卓UI模板,导入到AndroidStudio的时候提示MainActivity非法字符: '\ufeff' 解决方案,细细一想编译器没报错,但编译出错,应该是隐蔽字符BOM的问题,于是 ...

  4. skyline TerraBuilder 制作MPT方法与技巧(2)

    制作MPT的方法可以看这里<skyline TerraBuilder 制作MPT方法与技巧(1)>http://www.cnblogs.com/cannel/p/3622447.html ...

  5. C#操作MySQL数据库-----HelloWorld

    这里采用在visual studio 2010中通过MySql.Data.dll.MySql.Web.dll来连接mysql数据库, 之后便进行数据的插入和查询. Program.cs文件内容如下: ...

  6. null和空 not null

    所谓的NULL就是什么都没有,连\0都没有,\0在字符串中是结束符,但是在物理内存是占空间的,等于一个字节,而NULL就是连这一个字节都没有.在 数据库里是严格区分的,任何数跟NULL进行运算都是NU ...

  7. javascript 实现htmlEncode htmlDecode

    屌屌的写法..function htmlEncode(value){ //create a in-memory div, set it's inner text(which jQuery automa ...

  8. laravel controller:make

    php artisan make:controller DIR/XXXController

  9. Starship Troopers

    Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...

  10. Java中数组的快排

    描述输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符.   输入 第一行输入一个数N,表示有N组测试数据.后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成, ...