Test Instructions Analysis:
The topic is easier to understand, with d[i] to represent the optimal solution of the first I character, the state transfer equation d[i]=min{d[j]+1| [J+1~i] for palindrome}
The code is as follows:
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxs= +;7 int is[maxs+5][maxs+5];8 intDP[MAXS];9 CharS[MAXS];Ten voidPre_solve () { OneMemset is,0,sizeof is); A intlen=strlen (s); - for(intI=0; i<len;i++){ - intL=i; the intR=i; - while(l>=0&&r<Len) { - if(s[l]==S[r]) - isL [r]=1; + Else Break; -l--; +r++; A } at } - for(intI=0; i<len-1; i++){ - intL=i; - intr=i+1; - while(l>=0&&r<Len) { - if(s[l]==S[r]) in isL [r]=1; - Else Break; tol--; +r++; - } the } * } $ intMain () {Panax Notoginseng intT; -scanf"%d",&T); the while(t--){ +scanf"%s", s); A pre_solve (); thedp[0]=0; + intlen=strlen (s); - for(intI=1; i<=len;i++){ $dp[i]=1<< -; $ for(intj=0; j<i;j++){ - if( is[j] [I1]) -Dp[i]=min (dp[i],dp[j]+1); the } - }Wuyiprintf"%d\n", Dp[len]); the } -}
11584-partitioning by PALINDROMES--[DP]