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