Test instructions: give you a sequence of parentheses that asks you for the longest string length and number of matches.
Solution: Stack Simulation + DP
Problem Solving Code:
1 //File name:5c.cpp2 //Author:darkdream3 //Created time:2015 March 09 Monday 12:00 57 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - - using namespacestd; - Charstr[1000005]; - intEs61000005]; in intdp[1000005]; - structnode{ to intSi,val; + node () {} -Nodeint_si,int_val) the { *Si =_si; $val =_val;Panax Notoginseng } - }; theStack <node>St; + intMain () { Ascanf"%s", &str[1]); the intLen = strlen (&str[1]); + intTsum =0 ; -hs[0] =1; $ for(inti =1; I <= Len;i + +) $ { - if(Str[i] = ='(') - { theSt.push (Node (i,2)); - }Wuyi Else if(Str[i] = =')') the { - if(!st.empty ()) Wu { -Node TMP =st.top (); AboutDp[i] = Tmp.val + dp[tmp.si-1]; $Hs[dp[i]] + + ; - St.pop (); - if(!st.empty ()) - { ANode k =st.top (); + St.pop (); theSt.push (Node (k.si,k.val+tmp.val)); - } $ } the } the the } the for(inti =1000000; I >=0; I-- ) - { in if(Hs[i]) the { theprintf"%d%d\n", I,hs[i]); About Break; the } the } the return 0; +}
View Code
Codeforces 5C longest Regular bracket Sequence