[HIHO1300] The CARP flag (stack, DP) of the exhibition resort

Source: Internet
Author: User

Title Link: http://hihocoder.com/problemset/problem/1300

Give a string that contains only ' (' and ') ' and ask how many substring-like parentheses are matched.

The matching rules are depicted in the dry (Konjac) (the language) is very rotten!

Maintain a stack, save ' (' subscript. When encountering ') ' and the stack is not empty, the description has been matched to a pair of parentheses. Remove the top of the stack ' (' the position of IDX. and record at this time the state, DP (i) =DP (idx-1) +1.

1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> +  - using namespacestd; +  A //Kirai MAXN at Const intMAXN =1000010; - intN; - intST[MAXN], top; - intDP[MAXN]; - CharSTR[MAXN]; -  in intMain () { -     //freopen ("in", "R", stdin); to      while(~SCANF ("%s", str)) { +top =0; -n =strlen (str); theMemset (DP,0,sizeof(DP)); *          for(inti =0; I < n; i++) { $             if(Str[i] = ='(') st[top++] =i;Panax Notoginseng             Else { -                 if(Top >0) { the                     intIDX = st[--top]; +Dp[i] =1+ dp[idx-1]; A                 } the             } +         } -         intAns =0; $          for(inti =0; I < n; i++) { $printf"%d", Dp[i]); -Ans + =Dp[i]; -         } theprintf"%d\n", ans); -     }Wuyi     return 0; the}

[HIHO1300] The CARP flag (stack, DP) of the exhibition resort

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.