These days, I have been restless and it is not efficient to answer questions. It may also be difficult to dynamically plan the questions here. Sometimes it takes a long time to get a question, so it is very sad. One is that your IQ is poor.
This question is to give you a sequence, and find all the trees that follow this sequence.
In this way, we can implement recursive solutions, that is, every tree can be divided into left and right subtree. These two types of books can actually be seen as independent trees. Therefore, it is very good to solve subproblems.
The recursive equation is dp (I, j) = sum (dp (I + 1, k-1) * dp (K, j )). Of course, K is from 1-len-1. In this way, all the trees can be obtained. That is, if we use VIS-based memory-based search, we can also make d [I] =-1.
PostCode:
# Include <stdio. h> # include <string. h >#include <iostream >#include <string> using namespace STD; const int mod = 1000000000; char STR [333]; int d [333] [333]; int vis [333] [333]; int dp (int I, Int J) {If (vis [I] [J]) {return d [I] [J];} vis [I] [J] = 1; if (I = J) {d [I] [J] = 1; return 1;} If (STR [I]! = STR [J]) {d [I] [J] = 0; return 0;} Int & Ans = d [I] [J]; for (int K = I + 2; k <= J; k ++) {If (STR [I] = STR [k]) {ans = (ANS + (long) DP (I + 1, k-1) * (long) DP (K, j) % mod ;}} return ans;} int main () {While (scanf ("% s", STR )! = EOF) {int Len = strlen (STR); memset (VIS, 0, sizeof (VIS); memset (D, 0, sizeof (d )); printf ("% d \ n", dp (0, len-1);} // system ("pause"); Return 0 ;}