Poj 2955 brackets (interval DP)

Source: Internet
Author: User

Question:

The two matching brackets are used to find the longest matching length.


Train of Thought Analysis:

State equation: DP [I] [J] indicates the interval I ~ The longest matching length between J.

Transition equation: DP [I] [J] = max (DP [I + 1] [J], DP [I] [J-1], DP [I + 1] [k-1] + dp [k + 1] [J] + 2 (condition is I, K position match ))


# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define maxn 105 using namespace STD; char STR [maxn]; int DP [maxn] [maxn]; int main () {While (scanf ("% s", STR + 1 )! = EOF) {If (strcmp (STR + 1, "end") = 0) break; int Len = strlen (STR + 1); memset (DP, 0, sizeof DP); For (INT I = len-1; I> = 1; I --) {for (Int J = I + 1; j <= Len; j ++) {DP [I] [J] = max (DP [I + 1] [J], DP [I] [J-1]); For (int K = I + 1; k <= J; k ++) {If (STR [I] = '(' & STR [k] = ')') | (STR [I] = '[' & STR [k] = ']') DP [I] [J] = max (DP [I] [J], DP [I + 1] [k-1] + 2 + dp [k + 1] [J]); }}printf ("% d \ n ", DP [1] [Len]);} 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.