Hihocoder #1300: Carp flag DP at the exhibition resort

Source: Internet
Author: User

Topic Links:

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

Exercises

Use the stack to preprocess each ') ' matched ' (' seat, placed in the POS array.

Dp[i] Indicates the number of valid substrings ending with I, then the transfer equation is easy to know:

dp[i]=dp[pos[i]-1]+1;

Code:
#include <iostream>#include<cstdio>#include<stack>using namespaceStd;typedefLong LongLL;Const intMAXN = 1e6 +Ten;CharSTR[MAXN]; LL DP[MAXN];intPOS[MAXN];voidinit () {memset (pos,0,sizeof(POS)); Memset (DP,0,sizeof(DP));}intMain () { while(SCANF ("%s", str +1) ==1) {        intlen = strlen (str +1);        Init (); Stack<int>MS;  for(inti =1; I <= Len; i++) {            if(Str[i] = ='(') {Ms.push (i); }            Else {                if(!Ms.empty ()) {                    intj =ms.top (); Ms.pop (); Pos[i]=J; }}} LL ans=0;  for(inti =1; I <= Len; i++) {            if(pos[i]>=1) Dp[i] = dp[pos[i]-1] +1; Ans+=Dp[i]; } printf ("%lld\n", ans); }    return 0;}

Hihocoder #1300: Carp flag DP at 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.