题目:题目链接

思路:预处理出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. 切片操作:MATLAB VS Python

    切片操作:MATLAB VS Python 一.MATLAB 矩阵的拆分 1.冒号表达式: t = e1:e2:e3 e1表示初始值,e2为步长,e3为终止值(包括e3),产生一个从e1到e3,步长为 ...

  2. 轻松完成excel读写操作- 基于POI的框架BingExcel的使用(1)

    Bingexcel User Guide 使用maven进行项目开发目前项目的maven仓库是在github上,浏览地址为 https://github.com/bingyulei007/mvn-re ...

  3. Form上传编译

    编译上传的Form,使用命令: 在R12服务器上: cd $AU_TOP/forms/US frmcmp_batch module=$CUX_TOP/forms/ZHS/XXX.fmbuserid=a ...

  4. ListView优化的时候ViewHolder的简洁写法

    在ListVIew做复用优化的时候,经常会写ViewHolder,还需要很麻烦的去findview,我最讨厌写一堆的这样代码了,今天看到了一个极简的写法,很好用,很简洁啊!!! public stat ...

  5. eclipse中安装thymeleaf插件完成thymeleaf模板中自动代码提示功能

    插件地址:https://github.com/thymeleaf/thymeleaf-extras-eclipse-plugin 页面有介绍如何使用:

  6. Linux教程之:Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

    Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 使用命令关闭占用80端口的程序 sudo fuser - ...

  7. 如何使用ABSL代码调用Web service

    需求:在C4C UI里创建web service(maintain ticket),然后通过ABSL代码消费. 1. 创建一个新的Communication Arrangement 基于Manage ...

  8. while循环小例

    # 使用while 循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n <= 10: if n == 7: pass else: print(n) n = n + 1 # ...

  9. Uva 11732 strcmp()函数

    题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...

  10. sql server 基础

    1 .左连接 select a.* ,b.* from student as aleft join hobby as bon a.hobbyid=b.hobbyid 2. 右 连接 select a. ...