http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1154

1154 回文串划分

基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
有一个字符串S,求S最少可以被划分为多少个回文串。
例如:abbaabaa,有多种划分方式。
 
a|bb|aabaa - 3 个回文串
a|bb|a|aba|a - 5 个回文串
a|b|b|a|a|b|a|a - 8 个回文串
 
其中第1种划分方式的划分数量最少。
Input
输入字符串S(S的长度<= 5000)。
Output
输出最少的划分数量。
Input示例
abbaabaa
Output示例
3
原本以为N^2会爆炸后来发现好像也不会= =倒是题意搞懵逼我了,题意是必须全部划分为回文子串,求最少划分几个,我以为可以出现非回文子串,就是0......天真
dp[i]表示前i个字符的答案,第i个字符可能单独也可能与前面的某一串相连构成一个回文串,答案就是 dp[i]=MIN{dp[j]+1 | j<i&&j+1--i构成回文串}
 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
LL mod=1e9+;
int dp[];
char s[];
bool ok(int a,int b)
{
int m=b-((b-a)>>);
for(int i=a,j=b;i<=m;++i,--j)
if(s[i]!=s[j]) return ;
return ;
}
int main()
{
int N,i,j;
scanf("%s",s+);
N=strlen(s+);
for(i=;i<=N;++i)
{
dp[i]=dp[i-]+;
for(j=;j<i;++j)
{
if(ok(j+,i)&&dp[i]>dp[j]+) dp[i]=dp[j]+;
}
}
printf("%d\n",dp[N]);
return ;
}

51nod 1154 dp的更多相关文章

  1. 51nod 1154【DP】

    区间DP大暴力吧?GG. dp[ i ] 为字符至 i 的最少数量. 如果[Left , Right]是回文串, dp[Right] = min(dp[ Right ] , dp[Left-1] + ...

  2. 51Nod - 1154 回文串划分(最少回文串dp)

    回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式.   a|bb|aabaa - 3 个回文串 a|bb|a|aba|a - 5 个回文串 a|b ...

  3. 51NOD 1154 回文串的划分(DP)

    思路:参考了网上,思路很清奇,借助vis[i][j]来表示从i到j是否为回文串,回文串这边是用的双重循环来写的:dp[i]用来表示以i结尾的字符串最少的回文串有多长. #include<cstr ...

  4. 51nod 1270 dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1270 简单的线性dp,最近狂刷水题真的是...药丸 差值最大得话要么是峰 ...

  5. 51nod 1154 回文串划分

    1154 回文串划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有 ...

  6. 【51nod 1154】 回文串划分

    有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式. a|bb|aabaa - 3 个回文串 a|bb|a|aba|a - 5 个回文串 a|b|b|a|a|b ...

  7. 51nod 1412 AVL树的种类(dp)

    题目链接:51nod 1412 AVL树的种类 开始做的时候把深度开得过小了结果一直WA,是我天真了.. #include<cstdio> #include<cstring> ...

  8. 51nod 1183 编辑距离(dp)

    题目链接:51nod 1183 编辑距离 #include<cstdio> #include<cstring> #include<algorithm> using ...

  9. 51nod 1051 最大子矩阵和(dp)

    题目链接:51nod 1051 最大子矩阵和 实质是把最大子段和扩展到二维.读题注意m,n... #include<cstdio> #include<cstring> #inc ...

随机推荐

  1. (4.17)sql server中的uuid获取与使用

    sql server中的uuid  建表: 1.自增长 studentno int primary key identity(1,1)——bigint也是可以的 2.创建uuidcustomerid  ...

  2. SVG Use(转)

    转自:http://www.zhangxinxu.com/wordpress/2014/07/introduce-svg-sprite-technology/ 未来必热:SVG Sprite技术介绍 ...

  3. R中apply函数族

    参考于:http://blog.fens.me/r-apply/ 1. apply的家族函数 2. apply函数 apply函数是最常用的代替for循环的函数.apply函数可以对矩阵.数据框.数组 ...

  4. html当前文档的状态

    <script type="text/javascript"> document.onreadystatechange = loadingChange;//当页面加载状 ...

  5. Tensorflow 学习笔记(一)TensorFlow入门

    一.计算模型----计算图 1.1 计算图的概念:TensorFlow就是通过图的形式绘制出张量节点的计算过程,例如下图执行了一个a+b的操作. 1.2 计算图的使用 TensorFlow程序一般分为 ...

  6. js检测数组类型

    1.instanceof 当只有一个全局执行环境时适用,如果包含多个框架,就存在两个以上不同版本的Array构造函数,如果从一个框架向另一个框架传递数组,传入的数组与在第二个框架中原生创建的数组分别具 ...

  7. while小用

    1.使用while打印1 2 3 4 5 6  8 9 10 #!/usr/bin/env python #encoding: utf-8 num = 1 while num < 11: if ...

  8. Linux Shell编程第5章——文件的排序、合并和分割

    目录 sort命令 sort命令的基本用法 uniq命令 join命令 cut命令 paste命令 split命令 tr命令 tar命令 sort命令 sort命令是Linux系统一种排序工具,它将输 ...

  9. Servlet Rest

    http://www.blogjava.net/yongboy/archive/2010/10/01/333609.html

  10. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...