Interval DP basics-poj2955 -- brackets

Source: Internet
Author: User

Angry take a blood, first blood, the first interval DP, the first time it was like this, it was inexplicably lost ~~~


Although the question is a bird's language, it is still very naked to tell us that the maximum number of matching brackets is required. DP starts from the beginning ~

DP [I] [J] indicates the maximum number of matches in the interval [I, j ].State transition equationThat is:

DP [I] [J] = max (DP [I] [k] + dp [k + 1] [J])

Yes, firstInitialization BoundaryAh, two steps ~ :

Memset (DP, 0, sizeof DP );

If STR [I] = STR [I + 1]: DP [I] [I + 1] = 2 please refer to ----> This string ([] [] [) it's easy to understand.



Malicious Code:

# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; int DP [110] [110]; char s [110]; bool check (int I, Int J) // judge whether it matches {If (s [I] = '[' & S [J] = ']') return true; If (s [I] = '(' & S [J] = ') return true; return false;} int main () {While (scanf ("% s", S )! = EOF) {If (strcmp (S, "end") = 0) break; int L = strlen (s); memset (DP, 0, sizeof DP ); for (INT I = 0; I <L; I ++) {// initialize if (check (I, I + 1 )) {DP [I] [I + 1] = 2 ;}} for (INT P = 3; P <= L; P ++) {// enumeration Interval Length for (INT I = 0; I <= L-P; I ++) {// enumeration interval start point Int J = I + p-1; if (check (I, j) {DP [I] [J] = DP [I + 1] [J-1] + 2;} For (int K = I; k <j; k ++) {// divide the interval into two segments DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k] [J]) ;}} cout <DP [0] L-1] <Endl ;}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.