LightOJ 1044 Palindrome Partitioning(简单字符串DP)
A palindrome partition is the partitioning of a string such that each separate substring is a palindrome.
For example, the string "ABACABA" could be partitioned in several different ways, such as {"A","B","A","C","A","B","A"}, {"A","BACAB","A"}, {"ABA","C","ABA"}, or {"ABACABA"}, among others.
You are given a string s. Return the minimum possible number of substrings in a palindrome partition of s.
Input
Input starts with an integer T (≤ 40), denoting the number of test cases.
Each case begins with a non-empty string s of uppercase letters with length no more than 1000.
Output
For each case of input you have to print the case number and the desired result.
Sample Input
3
AAAA
ABCDEFGH
QWERTYTREWQWERT
Sample Output
Case 1: 1
Case 2: 8
Case 3: 5
题意:给出一个字符串,将该字符串切割成若干个回文串,使切割后的回文串数最小,求这个回文串数
题解:这道题其实在之前DP百题大过关中已经出现过了,复杂度是n^3的,今天写的时候突然糊出了n^2的做法,再记录一下
首先是状态转移方程的推导
f[i]表示1-i之间的最小分割数,很显然只有当i~j是回文串的时候i-1才能转移到j,贡献为1
所以状态转移方程为
f[j]=max{f[i-1]+1}(i~j为回文串)
这个转移方程是n^2的,但上次写的时候在里面加入了O(N)的回文串判断,复杂度变成了O(N^3)
如今想想,其实是不是回文串这东西是可以预处理的
枚举每一个中点,向两边拓展,可以在O(N^2)中处理出每一段i~j是否是回文串
然后就有了O(N^2)复杂度的DP
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int c[][],n,f[];
char s[]; void get(int l,int r)
{
while(l>&&r<=n)
{
if(s[l]==s[r])
{
c[l][r]=;
}
else
{
return ;
}
l--;
r++;
}
} int main()
{
int t,ttt=;
scanf("%d",&t);
while(t--)
{
ttt++;
memset(f,0x3f,sizeof(f));
memset(c,,sizeof(c));
scanf("%s",s+);
n=strlen(s+);
for(int i=; i<=n; i++)
{
get(i,i);
get(i,i+);
}
f[]=;
for(int i=; i<=n; i++)
{
for(int j=; j<=i; j++)
{
if(c[j][i])
{
f[i]=min(f[i],f[j-]+);
}
}
}
printf("Case %d: %d\n",ttt,f[n]);
} }
大佬们的常数都好优越啊,N^3跑的都比N^2快qwq
LightOJ 1044 Palindrome Partitioning(简单字符串DP)的更多相关文章
- Lightoj 1044 - Palindrome Partitioning (DP)
题目链接: Lightoj 1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...
- lightoj 1044 - Palindrome Partitioning(需要优化的区间dp)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 题意:求给出的字符串最少能分成多少串回文串. 一般会想到用区间dp暴力3个for ...
- Light oj 1044 - Palindrome Partitioning(区间dp)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 #include ...
- 1044 - Palindrome Partitioning(区间DP)
题目大意: 给你一个字符串,问这个字符串最少有多少个回文串. 区间DP直接搞 #include<cstdio> #include<cstring> #include&l ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- atcoder 2017Code festival C ——D题 Yet Another Palindrome Partitioning(思维+dp)
题目大意: 把一个字符串s分割成m个串,这m个串满足至多有一种字符出现次数为奇数次,其他均为偶数次,问m的最小值 题解: 首先我们想一下纯暴力怎么做 显然是可以n^2暴力的,然后dp[i]表示分割到i ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
随机推荐
- 优化笔记:jsyhjkzqxx_D_20140916.gz
有几张表没有权限,所以跑不起来. 目测黄色部分比较坑爹,死了n多脑细胞才看懂,又死了n多脑细胞才改出来.对5034进行了2次扫描,并多次分组排序求和.(分组和排序算法相对来说比较耗性能) 改为只扫描一 ...
- OD 实验(十七) - 对一个程序的逆向分析
程序: 运行程序 弹出一个对话框,点击 OK 来到主界面,点击 Help -> Register Now 这是输入注册码的地方 按关闭程序的按钮 会提示剩下 30 天的使用时间 用 Ressco ...
- [Python] Regular Expressions
1. regular expression Regular expression is a special sequence of characters that helps you match or ...
- Linux常用命令及使用方法
一.Linux命令格式 command [-option] [parameter1] ..... 即:命令名 [选项] [参数] 选项:控制命令 参数:可有0个,1个或多个 二.常用命令及用法 1.与 ...
- 回归问题中代价函数选择的概率解释(Probabilistic interpretation)
在我们遇到回归问题时,例如前面提到的线性回归,我们总是选择最小而成作为代价函数,形式如下: 这个时候,我们可能就会有疑问了,我们为什么要这样来选择代价函数呢?一种解释是使我们的预测值和我们训练样本的真 ...
- 【Oracle】Oracle数据库DATABASE LINK 的命名
当前数据库的GLOBAL_NAMES参数设置为 TRUE,使用DATABASE LINK 时,DATABASE LINK的名称必须与被连接库的 GLOBAL_NAME一致. 而要建多个 DBLINK到 ...
- python的disutils创建分发包
python中的distutils包主要用创建共享包,安装包,在平时安装python模块的时候,使用的命令如下: python setup.py install 其实以上代码就是distuitls包提 ...
- ffmpeg强制使用TCP方式读取rtsp流
ffmpeg强制使用TCP方式处理rtsp流,参考网上资料,得知可以使用如下命令: “ffmpeg -rtsp_transport tcp -i rtsp://admin.......” 可以是使用抓 ...
- centos7 端口开放
CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,google之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用firewalld ...
- 使用RampTexture实现BRDF效果
[使用RampTexture实现BRDF效果] BRDF stands for bidirectional reflectance distribution function. While that ...