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 ...
随机推荐
- 两栏自适应布局,右侧div宽高不定
.main,.sitebar{ height: 300px; font: bolder 20px/300px "微软雅黑"; color: bl ...
- 关于Python安装官方whl包和tar.gz包的方法详解
Windows环境: 安装whl包:pip install wheel -> pip install **.whl 安装tar.gz包:cd到解压后路径,python setup.py inst ...
- TypeScript--deno前置学习
第一节:前言: 1.JavaScript 的发展简史图: 2.关于node与deno: (1)Node与Deno 的区别:Node 可以工作,而 Deno 不行,Deno 只是一个原型或实验性产品. ...
- c++ 字符检测 TCharacter
c++ 字符检测 IsSurrogatePair,IsHighSurrogate,IsLowSurrogate,ConvertToUtf32http://docwiki.embarcadero.com ...
- 跟我学算法聚类(kmeans)
kmeans是一种无监督的聚类问题,在使用前一般要进行数据标准化, 一般都是使用欧式距离来进行区分,主要是通过迭代质心的位置 来进行分类,直到数据点不发生类别变化就停止, 一次分类别,一次变换质心,就 ...
- VB导入Excel到数据库软件(持续更新中。)
1.选择Excel文件版本 电脑上用的 Office2010 引用:Mircosoft Excel 14.0 Object Library 2.选择Excel文件 '选择文件公共变量 Public D ...
- PHP - 闭合标签
最最开始的时候经常遇到这个问题,就是如果一个文件里面全部都是php代码的话,我写了前闭合和后闭合的时候,文件一多就容易报错,老是说什么有关输出的错误,貌似大概就是header已经发了. 手册上面这个样 ...
- Lists、Sets、Maps和Collections2的使用
1.Lists //Lists System.out.println("### Lists ###"); ArrayList<String> arrayList = L ...
- LeetCode之位操作题java
191. Number of 1 Bits Total Accepted: 87985 Total Submissions: 234407 Difficulty: Easy Write a funct ...
- win7下IIS的安装和配置图文教程
1. 首先是安装IIS.打开控制面板,找到”程序与功能”,点进去 2. 点击左侧”打开或关闭Windows功能” 3. 找到”Internet 信息服务”,按照下图打勾即可 等待安装完成 4. 安装完 ...