LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串。
一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时候要枚举,这样时间复杂度是不可行的。
然后我就想降维度了,只能线性DP,dp[i]表示子串[0,i]的答案。这样可以从i-1转移到i,str[i]单独作一段或者str[i]能和前面的组成回文串,方程如下:
dp[i]=min(dp[i-1]+1,dp[j-1]+1) (子串[j,i]是回文串)
现在问题是怎么快速判断一个字符串的任意子串是否是回文串。
我想该不会要用字符串的一些数据结构或算法吧。。忽然又想到区间DP,这个问题是可以用区间DP解决的:
dp2[i][j]表示子串[i,j]是否是回文串
而转移只要一步即可:
dp2[i][j] = (str[i]==str[j] && dp2[i+1][j-1])
因此这就可以在O(strlen2)预处理完并在O(1)时间复杂度下判断任意区间是否是回文串。
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- char str[];
- bool palindrome[][];
- int d[];
- int main(){
- int t;
- scanf("%d",&t);
- for(int cse=; cse<=t; ++cse){
- scanf("%s",str);
- int n=strlen(str);
- for(int i=; i<n; ++i){
- for(int j=; j<n; ++j){
- palindrome[i][j]=(i>=j);
- }
- }
- for(int len=; len<=n; ++len){
- for(int i=; i+len<=n; ++i){
- if(str[i]==str[i+len-] && palindrome[i+][i+len-]) palindrome[i][i+len-]=;
- }
- }
- d[]=;
- for(int i=; i<n; ++i){
- if(palindrome[][i]){
- d[i]=;
- continue;
- }
- d[i]=d[i-]+;
- for(int j=i-; j>=; --j){
- if(palindrome[j][i]) d[i]=min(d[i],d[j-]+);
- }
- }
- printf("Case %d: %d\n",cse,d[n-]);
- }
- return ;
- }
LightOJ1044 Palindrome Partitioning(区间DP+线性DP)的更多相关文章
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- Atcoder Yet Another Palindrome Partitioning(状压dp)
Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...
- hdu1712 线性dp
//Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- 1. 线性DP 300. 最长上升子序列 (LIS)
最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...
- 1044 - Palindrome Partitioning(区间DP)
题目大意: 给你一个字符串,问这个字符串最少有多少个回文串. 区间DP直接搞 #include<cstdio> #include<cstring> #include&l ...
- Lightoj 1044 - Palindrome Partitioning (DP)
题目链接: Lightoj 1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 131. Palindrome Partitioning (Back-Track, DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- 怎样从命令行进入mac桌面
www.iwangzheng.com 刚买的mac电脑,想在终端进入桌面,可以用下面的方式 点击桌面右上方的放大镜搜索到Terminal并打开,输入 $ cd /Users 这里会显示多个用户,进入自 ...
- Stanford机器学习---第四讲. 神经网络的表示 Neural Networks representation
原文 http://blog.csdn.net/abcjennifer/article/details/7749309 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- Balanced Teams (USACO Jan Bronze 2014)
既然是bronze,毫无压力的AC了. 就是个深搜,当然加个剪枝--最后一个组不用搜. 恩可以一个一个组分层次dfs,这样会跑得飞起~~也不容易错 #include <cstdio> in ...
- 《ASP.NET1200例》ASP.Net 之Datalist数据删除(支持批量)
.aspx <div> <asp:DataList ID="DataList1" runat="server" Width="355 ...
- poj 1833
http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排 ...
- iOS tableview 选中Cell后的背景颜色和文字颜色
做下记录,备忘 改文字颜色其实是UILabel的属性,改背景颜色是cell的属性,都和tableview无关. cell.textLabel.textColor = BAR_COLOR; cell.t ...
- 1.django笔记之django基础
一.django简介 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内 ...
- Python -- BeautifulSoup的学习使用
BeautifulSoup4.3 的使用 下载和安装 # 下载 http://www.crummy.com/software/BeautifulSoup/bs4/download/ # 解压后 使用r ...
- .Net查看项目文件弹出未找到与约束
项目能打开,但是当要在项目中查看文件时弹出未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService ...
- index index.html index.htm index.php
server { listen 80; server_name localhost; index index.html index.htm index.php;#前后顺序有关系,越在前优先级别越高 r ...