题目:题目链接

思路:预处理出l到r为回文串的子串,然后如果j到i为回文串,dp[i] = min(dp[i], dp[j] + 1)

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <cmath> #define FRER() freopen("in.txt", "r", stdin)
#define FREW() freopen("out.txt", "w", stdout)
#define INF 0x3f3f3f3f using namespace std; const int maxn = + ; int n, len, l, r;
char str[maxn]; bool check[maxn][maxn];
int dp[maxn]; int main()
{
//FRER();
scanf("%d", &n);
while(n--) {
scanf("%s", str);
len = strlen(str);
memset(check, , sizeof(check)); for(int i = ; i < len; ++i) {
l = r = i;
while(l >= && r < len) {
if(str[l] == str[r]) {
check[l + ][r + ] = ;
--l; ++r;
}
else break;
}
l = i;
r = i + ; while(l >= && r < len) {
if(str[l] == str[r]) {
check[l + ][r + ] = ;
--l; ++r;
}
else break;
}
} memset(dp, , sizeof(dp));
for(int i = ; i <= len; ++i) {
dp[i] = dp[i - ] + ;
for(int j = ; j < i; ++j) {
if(check[j][i])
dp[i] = min(dp[i], dp[j - ] + );
}
} printf("%d\n", dp[len]);
}
return ;
}

Partitioning by Palindromes UVA - 11584 简单dp的更多相关文章

  1. UVA 11584 入门DP

    一开始把它当成暴力来做了,即,从终点开始,枚举其最长的回文串,一旦是最长的,马上就ans++,再计算另外的部分...结果WA了 事实证明就是一个简单DP,算出两个两个点组成的线段是否为回文,再用LCS ...

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

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

  3. uva 10648(简单dp)

    Recently one of my friend Tarik became a member of the food committee of an ACM regional competition ...

  4. Uva 11078 简单dp

    题目链接:http://uva.onlinejudge.org/external/110/11078.pdf a[i] - a[j] 的最大值. 这个题目马毅问了我,O(n^2)超时,记忆化一下当前最 ...

  5. UVA 111 简单DP 但是有坑

    题目传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18201 其实是一道不算难的DP,但是搞了好久,才发现原来是题目没 ...

  6. uva 11584 - 字符串 dp

    题目链接 一个长度1000的字符串最少划分为几个回文字符串 ---------------------------------------------------------------------- ...

  7. uva 11584 Partitioning by Palindromes 线性dp

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

  8. 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 ...

  9. 区间DP UVA 11584 Partitioning by Palindromes

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

随机推荐

  1. Use Exception.ToString() instead of Exception.Message.

    Exception.Message contains only the message (doh) associated with the exception. Example: Object ref ...

  2. Oracle:Start with connect by prior 递归

    SELECT * from CONNECT BY {PRIOR列名1=列名2|列名1=PRIOR列名2} [START WITH]; Oracle的递归查询:     START WITH :描述开始 ...

  3. php的yii框架开发总结10

    1.CActiveForm是Chtml类的封装,但是它有数据验证的功能,有三种方式:服务器端.客户端.Ajax数据验证. 服务器端验证:当整个表单页面被提交后,在服务器端 进行验证.如果存在任何验证错 ...

  4. System Center Configuration Manager 2016 必要条件准备篇(Part4)

    步骤4.重新启动Configuration Manager主服务器 注意:在Configuration Manager服务器(CM01)上以本地管理员身份执行以下操作 打开管理命令提示符并发出以下 ...

  5. HCNA配置浮动静态路由

    1.拓扑图 2.配置IP R1 Please press enter to start cmd line! ############ <Huawei> Dec ::-: Huawei %% ...

  6. tomcat8.5配置优化

    1.应用程序安全&关闭自动部署 默认值: <Host name="localhost" appBase="webapps" unpackWARs= ...

  7. ubuntu修改字体大小

    字体大小可以整体修改 System Settings -> Displays -> Scale for menu and title bars

  8. 关于硬盘分区使用exFat格式的优势及劣势(含摘抄)

    优势 可以设置最大32M的簇: 不记录日志. 劣势 无法使用windows的“文件共享”: 通过近期某个文件数量密级任务的测试发现,在大量文件的处理性能上,NTFS比exFAT文件系统的性能高出不少. ...

  9. web前端的10个顶级CSS UI开源框架

    随着CSS3和HTML5的流行,我们的WEB页面不仅需要更人性化的设计理念,而且需要更酷的页面特效和用户体验.作为开发者,我们需要了解一些宝贵的CSS UI开源框架资源,它们可以帮助我们更快更好地实现 ...

  10. JavaRebel 2.0 发布,一个JVM插件

    JavaRebel是一个JVM插件(-javaagent),能够即时重载java class更改,因此不需要重新部署一个应用或者重启容器,节约开发者时间. JavaRebel 2.0的新特征: 改变了 ...