Carp flag at the Hihocoder 1300 Exhibition Resort (bracket matching problem DP)

Source: Internet
Author: User

#1300: The time limit of the CARP flag in the exhibition resort:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

Yanshou County North listed "North City Park Exhibition Resort", is one of the three major cherry blossom Mutsu. Every year from mid-April to early May, there will be a grand memorial ceremony. In addition to the cherry blossom trails, you can take the sightseeing wagon Xu Xing, and the CARP flag across the North River, along with the local special borrowing strategy to worship the ancestors and the North Ghost Sword Dance.

Let's say we use a parenthesis string containing ' (', ') ' to distinguish the direction of each side of the CARP flag. A sequence of parentheses is called legal when and only if two conditions are met: first, for the entire sequence, the number of opening brackets equals the closing parenthesis; for any prefix, the number of opening brackets is not less than the number of closing parentheses. Island Niang want to know, for a string of parentheses, how many substrings are legal, you can help her.

Input

The input data is only one row, containing a length of n (1?≤? n ?? ≤?106) the parentheses string.

Output

Outputs a row that represents the number of valid parentheses substrings.

Sample input
(()())
Sample output
4
Idea: The number of opening brackets equals the number of closing parentheses, and the number of open parentheses to each position is greater than or equal to the right parenthesis. Each closing parenthesis can be enumerated to the current number of consecutive valid matching parentheses. The number of valid parentheses to the current closing parenthesis is equal to the number of consecutive valid parentheses to the last parenthesis of the opening parenthesis that matches it +1. Finally, add up is answer.
code time to:
#include <iostream> #include <cstdio> #include <queue> #include <cstring>using namespace std; const int MAXN = 1e6+100;typedef long long LL; #define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1int DP[MAXN] ; int Id[maxn];char Str[maxn];int main () {memset (dp,0,sizeof dp);//memset (sum,0,sizeof sum); int i,j,k;scanf ("%s", str+1 ); int L=strlen (str+1); LL ans=0;int sum=0;int tmp=0;for (i=1;i<=l;i++) {if (str[i]== ' (') {id[tmp++]=i;} else {if (tmp>0) {tmp--;sum=dp[id[tmp]-1]+1;dp[i]=sum;ans=ans+sum;}}} Cout<<ans<<endl;return 0;}

WA Code:
Examples of WA: ((()) (()) output 7 The correct answer is 6. The parentheses sequence is often easy to ignore in the () (()) similar bracket sequence.
#include <iostream> #include <cstdio> #include <queue> #include <cstring>using namespace std; const int MAXN = 1e6+100;typedef long long LL; #define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1int DP[MAXN] ; Char Str[maxn];int main () {memset (dp,0,sizeof dp);//memset (sum,0,sizeof sum); int i,j,k;scanf ("%s", str+1); int l= Strlen (str+1); LL ans=0;int sum=0;int tmp=0;for (i=1;i<=l;i++) {if (str[i]== ' (') {dp[i]=0;sum=0;tmp++;} else {if (tmp>0) {sum=dp[i-2-sum*2]+1;dp[i]=sum;ans=ans+sum;} Tmp--;tmp=max (tmp,0);}} Cout<<ans<<endl;return 0;}


Carp flag at the Hihocoder 1300 Exhibition Resort (bracket matching problem DP)

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.