标签:style blog color os io strong 数据 for
6 (())() 3 ))(
6 1 0 1
用一个bool型的数组来标记匹配情况。
1 #include <iostream> 2 #include <stack> 3 #include <string> 4 #include <cstring> 5 using namespace std; 6 7 int n; 8 string s; 9 10 void getRes() { 11 bool a[s.length()]; 12 memset(a, false, s.length()); 13 stack<int> st; 14 for (int i = 0; i < s.length(); ++i) { 15 if (s[i] == ‘(‘) { 16 st.push(i); 17 } else { 18 if (!st.empty()) { 19 a[i] = true; 20 a[st.top()] = true; 21 st.pop(); 22 } 23 } 24 } 25 26 int max = 0, cnt = 1, tmp = 0; 27 for (int i = 0; i < s.length(); ++i) { 28 if (a[i]) { 29 ++tmp; 30 } else { 31 tmp = 0; 32 } 33 if (max == tmp && max != 0) { 34 ++cnt; 35 } else if (max < tmp) { 36 max = tmp; 37 cnt = 1; 38 } 39 } 40 cout << max << " " << cnt << endl; 41 } 42 43 int main() { 44 while (cin >> n) { 45 cin >> s; 46 getRes(); 47 } 48 return 0; 49 } 50 51 /************************************************************** 52 Problem: 1337 53 User: hupo250 54 Language: C++ 55 Result: Accepted 56 Time:310 ms 57 Memory:7604 kb 58 ****************************************************************/
[Jobdu] 题目1337:寻找最长合法括号序列,布布扣,bubuko.com
标签:style blog color os io strong 数据 for
原文地址:http://www.cnblogs.com/easonliu/p/3899543.html