UVA11584 Partitioning by Palindromes
Posted 2855669158
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11584 Partitioning by Palindromes相关的知识,希望对你有一定的参考价值。
题意:一个字符串(l<1000),问最少能分多少个回文串
题解:dp[i]代表前i个字符串最少的回文串,可以由前面的dp递推,dp[i] = max(dp[i],dp[i-t]+t);(1<t<=i)这里是n^2,判断回文串用Manacher
#include <bits/stdc++.h> #define maxn 100100 #define INF 0x3f3f3f3f typedef long long ll; using namespace std; char s[maxn], s1[maxn]; int p[2*maxn], dp[maxn]; int init(){ int j = 2, l=strlen(s); s1[0] = ‘&‘;s1[1] = ‘#‘; for(int i=0;i<l;i++){ s1[j++] = s[i]; s1[j++] = ‘#‘; } s1[j] = 0; return j; } void manacher(char *s){ int id=0,ma=0,mlen=-1; int len = init(); for(int i=0;i<len;i++){ if(i<ma) p[i] = min(p[2*id-i], ma-i); else p[i] = 1; while(s1[i-p[i]] == s1[i+p[i]]) p[i]++; if(i+p[i] > ma){ id = i; ma = i+p[i]; } } } int main(){ int T, l; scanf("%d", &T); while(T--){ scanf("%s", s); l = strlen(s); manacher(s); for(int i=2;i<=2*l;i+=2){ dp[i] = i/2; for(int j=0;j<i;j+=2){ if(p[(i+j+2)/2]-1 >= (i-j)/2) dp[i] = min(dp[i], dp[j]+1); } } printf("%d\n", dp[l*2]); } return 0; }
以上是关于UVA11584 Partitioning by Palindromes的主要内容,如果未能解决你的问题,请参考以下文章
UVA11584-Partitioning by Palindromes(动态规划基础)
题解UVA11584 Partitioning by Palindromes
uva 11584 Partitioning by Palindromes 线性dp
UVA 11584Partitioning by Palindromes