Algorithm competition-Training Guide Chapter 2-2.4-La 3516

Source: Internet
Author: User

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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.