Title Link: https://uva.onlinejudge.org/external/115/11584.pdf
Test instructions
A string, dividing it, so that each string is a palindrome string, the minimum number of palindrome string.
Analysis:
D (i) to the first character of the optimal solution (that is, at least divided into several palindrome string), there is the equation D (i) = min (d (j)) + 1; (where S[j+1,i] is a palindrome).
In this way, the enumeration is O (n^2) complexity, if according to ordinary judgment S[j+1,i] is a palindrome string, time complexity is O (n^3), first with O (n^2) of the complexity of preprocessing is_huiwen[i][j] to determine whether a palindrome string. The DP scheme I used earlier. Here's a better way to write--the center extension.
Note Here is: reserve a dp[0] = 0, so that there is no palindrome string in the current face, and the entire string is a palindrome string, there is d[i] = d[0]+1 = 1;
#include <bits/stdc++.h>using namespacestd;Const intMAXN = ++5;intN,KASE,P[MAXN][MAXN],D[MAXN];BOOLVIS[MAXN][MAXN];CharS[MAXN];intIs_palindrome (intIintj) { if(I >= J)return 1; if(S[i]! = S[j])return 0; if(Vis[i][j] = =true)returnP[i][j]; VIS[I][J]=true; P[I][J]= Is_palindrome (i+1, J-1); returnp[i][j];}intMain () {intT; scanf ("%d",&T); for(Kase =1; kase<=t; kase++) {memset (Vis,0,sizeof(VIS)); scanf ("%s", s+1); N= strlen (s+1); d[0] =0; Is_palindrome (1, N); for(intI=1; i<=n; i++) {D[i]= i+1; for(intj=0; j<i; J + +) { if(Is_palindrome (j+1, i)) D[i]= Min (d[i],d[j]+1); }} printf ("%d\n", D[n]); } return 0;}
Uva 11584, divided into palindrome string