the CARP flag of the exhibition resortTime limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
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 and contains a n n parenthesis string of length (1≤ ≤106).
Output
Outputs a row that represents the number of valid parentheses substrings.
-
-
Sample input
-
-
(()())
-
-
Sample output
-
4
AC Code:
#include <bits/stdc++.h>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong LongLL;ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=2e6+5;intN,a[n],dp[n],sum,qu[n];CharStr[n];intMain () {scanf ("%s", str); intlen=strlen (str); RIOP (len) {if(str[i]=='(') a[i+1]=1; Elsea[i+1]=-1; } LL ans=0; Sum=0; Riep (len) {if(a[i]>0) {sum++; Qu[sum]=i; } Else { if(sum>0) {Dp[i]=dp[qu[sum]-1]+1; Sum--; Ans+=(LL) dp[i]; } }} cout<<ans<<"\ n"; return 0;}
Carp Flag (DP) at the HIHOCODER-1300 exhibition Resort