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 ...
随机推荐
- js控制手机震动
js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...
- 08_java超市管理系统
超市管理系统功能介绍 * A:超市管理系统功能介绍 * a: 显示主菜单 ============欢迎光临ItCast超市============ 1: 货物 清单 2: 添加货物 3: 删除货物 4 ...
- leetcode503
public class Solution { public int[] NextGreaterElements(int[] nums) { int n = nums.Length; int[] ne ...
- java第三方类库实现图片等比缩放
public class ThumbnailTest { public static void main(String[] args) { InputStream is = null; try { / ...
- overflow与underflow
是新近的firefox浏览器中支持overflow, underflow这两个事件,当某一元素的大小超出父元素的显示范围就会触发overflow事件,如果从超出显示再变回不超出的状态则触发underf ...
- Windows安装Mysql5.7.22
1.下载Mysql,5.7版本,将zip包解压到某个安装目录下面,最好不要放C盘,选择一个容量大的磁盘.下载地址:https://dev.mysql.com/downloads/mysql/ 2.进入 ...
- drbd switch off
DRBD secondary to primary: drbdadm disconnect all drbdadm primary r0 --force mount /dev/drbd0 /mnt [ ...
- web前端整套面试题(一)--js相关
一.单选 1.以下哪条语句会产生运行:(A) A.var obj = ( ); B.var obj = [ ]; C.var obj = { }; D.var obj = / /; B代表数组,C代表 ...
- java算法 蓝桥杯 文化之旅
问题描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可能有相同的文化.不同文化 ...
- Nginx源码完全注释(5)core/ngx_cpuinfo.c
/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ...