Give a string, ask there are several ways to delete characters so that the deleted non-empty string is a palindrome string.
Of course the interval dp:DP[I][J] Indicates the number of STRI...STRJ of the substring
Feel bad transfer, may repeat forget. I hand over the "AAA" this data, and then the way to move:
D[I][J] =∑d[i '][j ']+1 (i<=i ' <=j ' <=j and Str[i]==str[j])
This interval DP is the same as the classical example of adjacent stones, enumeration length and then the lifting point, so as to ensure the topological order of the computational process. Time complexity O (STRLEN4).
1#include <cstdio>2#include <cstring>3 using namespacestd;4 Charstr[ the];5 Long Longd[ the][ the];6 intMain () {7 intT;8scanf"%d",&t);9 for(intCse=1; cse<=t; ++CSE) {Tenscanf"%s", str); One intn=strlen (str); Amemset (D,0,sizeof(d)); - for(intlen=1; len<=n; ++Len) { - for(intk=0; k+len<=n; ++k) { thed[k][k+len-1]=Len; - for(intI=0; i<len; ++i) { - for(intj=i+1; j<len; ++K) { - if(Str[k+i]!=str[k+j])Continue; +d[k][k+len-1]+=d[k+i+1][k+j-1]+1; - } + } A } at } -printf"Case %d:%lld\n", cse,d[0][n-1]); - } - return 0; -}
LightOJ1025 The Specials Menu (interval dp)