dp回文
。dp回文子串 通常在dp数组中存放的是 从i到j是否是回文子串
1.动态规划
2.中心扩展法
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
][] = { };
int main(void)
{
string s1;
while (cin >> s1)
{
int length = s1.length();
int i;
; i <= length - ; ++i)
dp[i][i] = ;
int j;
; i >=; --i)
{
; j <=length-; ++j)
{
if (s1[i] == s1[j])
{
dp[i][j] = dp[i + ][j - ] + ;
}
else
{
dp[i][j] = max(dp[i + ][j], dp[i][j - ]);
}
}
}
cout << ][length - ] << endl;
}
;
}
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
][] = { };
int main(void)
{
string s1;
while (cin >> s1)
{
int length = s1.length();
int i;
; i <= length - ; ++i)
dp[i][i] = ;
int j;
; i >= ; --i)
{
; j <= length - ; ++j)
{
if (s1[i] != s1[j])
{
dp[i][j] = dp[i + ][j] + dp[i][j - ] - dp[i + ][j - ];
}
else
{
dp[i][j] = dp[i + ][j] + dp[i][j - ] - dp[i + ][j - ] + dp[i + ][j - ] + ;
}
}
}
cout << ][length - ] << endl;
}
;
}
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
]; //长度
bool ispalindrome(string s,int start,int end)
{
while (start <= end)
{
if (s[start] != s[end])
{
return false;
}
++start;
--end;
}
return true;
}
int main(void)
{
string s;
while (cin >> s)
{
int length = s.length();
int i;
; i <= length; ++i)
{
dp[i] = i - ;
, i-) == true)
{
dp[i] = ;
}
}
int j;
; i <= length-; ++i)
{
; j <= i; ++j)
{
if (ispalindrome(s, j, i) == true)
{
dp[i + ] = min(dp[i + ], dp[j] + );
}
}
}
cout << "需要分割的次数为" << dp[length] << "次"<<endl;
}
;
}
dp回文的更多相关文章
- poj 1159 dp回文串
题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include ...
- POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- HDU4745--区间DP+回文串
这题的题意为,给你一个环状的字符串,有两只兔子分别从某任意的石头上开始跳跃.一只顺时针跳.一只逆时针跳.两只兔子每一次落脚处石头的质量都相同.兔子要一步一步的跳,且不能跳到之前跳到过的地方.总的来说, ...
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)
Times:5000ms: Memory limit:262144 kB 给定字符串S(|S|<=5000),下标由1开始.然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间 ...
- Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)
题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...
- GDUT 校赛02 dp回文串
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABSkAAAIhCAIAAAAtmainAAAgAElEQVR4nOzdfaxkd33n+ZJacstqa3 ...
- Lightoj1205——Palindromic Numbers(数位dp+回文数)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
随机推荐
- Java 合并、拆分PDF文档
处理PDF文档时,我们可以通过合并的方式,来任意组几个不同的PDF文件或者通过拆分将一个文件分解成多个子文件,这样的好处是对文档的存储.管理很方便.下面将通过Java程序代码介绍具体的PDF合并.拆分 ...
- 【MySQL】MySQL的执行计划及索引优化
我们知道一般图书馆都会建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的. ...
- noi.ac#309 Mas的童年(子集乱搞)
题意 题目链接 Sol 记\(s_i\)表示前\(i\)个数的前缀异或和,我们每次相当于要找一个\(j\)满足\(0 < j < i\)且\((s_i \oplus s_j) + s_j\ ...
- git 提交项目代码到码云步骤 以及出现错误解决办法
git initgit remote add origin 项目地址git add .git commit -m "注释"git push origin master 出现错误 $ ...
- vue环境搭建及项目介绍
搭建开发环境(搭建开发环境前必须安装node.js): 1.安装vue脚手架工具 $ npm install -g vue-cli 2.创建项目(注意项目名字不要有大写字母) vue init < ...
- (办公)mybatis工作中常见的问题(不定时更新)
1.mybatis的like查询的方式. <if test="shopName != null and shopName != ''"> <bind name=& ...
- Redis入门教程(一)
一.NoSQL概述 1.什么是NoSQL NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯 ...
- 如何连接别人电脑上的Oracle数据库?
需要一些前提条件: 1.对方的主机能被连接(如在同一局域网内) 2.需要知道对方数据库IP.端口号.服务名 3.需要知道对方提供的管理数据库的用户名及密码 连接方法: 1.在本地的oracle安装目录 ...
- MYSQL内置MYSQL数据库中你可以得到的信息
1:help_topic 可以查看函数帮助,例如:SELECT * from help_topic WHERE name='concat' 可以查看concat函数. 2:SLOW_LOG 慢查询日 ...
- slice 与 splice 的区别
slice: 定义一个数组:let b = ['a','b','c','d','e'] b:["a", "b", "c", "d& ...