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 ...
随机推荐
- 证件照制作:使用PS打印一寸照片
1. 打开文件,文件-打开-选择要操作的图片: 2. 修改图片尺寸,图像-图像大小,弹出对话框后,设置 宽度:2.5cm,高度:3.5cm,分辨率:300 像素/英寸: 3. 为要打印的照片设置白边, ...
- ORA-00600: 内部错误代码, 参数: [qctcte1]
[情景再现] 生产环境,JAVA程序某功能报错: ORA-00600: 内部错误代码, 参数: [qctcte1], [0], [], [], [], [], [], [] [问题排查] 1.检查Or ...
- krpano资源下载及还原全景图
krpano资源下载及还原全景图 现在全景云平台有大量的全景图资源,就存在了如何下载的需求. 原理:1.云平台多数使用krpano内核,首先需要将全景云平台中被krpano切成的全景图碎片下载下来,并 ...
- SignalR web实时同步 消息推送 广播
源码:https://github.com/SignalR/SignalR demo:http://download.csdn.net/download/qq_21533697/9702791#com ...
- MySQL数据库篇之pymysql模块的使用
主要内容: 一.pymysql模块的使用 二.pymysq模块增删改查 1️⃣ pymsql模块的使用 1.前言:之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库, 那如何在p ...
- Android Studio 无法正确引入包内存在的类
Android Studio 无法识别同一个 package 里的类,显示为红色,但是 compile 没有问题. 重启,rebuild,clean都没有用. 多半是因为 Android Studio ...
- VUE+WebPack实现精美Html5游戏设计:纸牌战争
- 基于Mybatis分页插件PageHelper
基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...
- 270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点
[抄题]: Given a non-empty binary search tree and a target value, find the value in the BST that is clo ...
- Java 依赖注入标准(JSR-330)简介
作者:88250 ,Vanessa 时间:2009 年 11 月 19 日 Java 依赖注入标准(JSR-330,Dependency Injection for Java)1.0 规范已 ...