-
Notifications
You must be signed in to change notification settings - Fork 2
/
32.cpp
101 lines (90 loc) · 2.46 KB
/
32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Author : Accagain
// Date : 17/2/17
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given a string containing just the characters '(' and ')',
*
* find the length of the longest valid (well-formed) parentheses substring.
*
* For "(()", the longest valid parentheses substring is "()", which has length = 2.
*
* Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
*
* 做法:
* 有点类似于KMP算法,保存(的位置,并且保存每一个)能达到的位置
* 特殊样例:
* ()((()()()()()))
*
* 时间复杂度:
* o(n)
*
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
struct Info
{
char c;
int pos;
Info(int c, int pos) : c(c), pos(pos) {};
};
int longestValidParentheses(string s) {
int ans = 0;
int next[s.size()];
memset(next, -1, sizeof(next));
stack<Info*> mys;
for(int i=0; i<s.size(); i++)
{
if(s[i] == '(')
{
Info * now = new Info(s[i], i);
mys.push(now);
next[i] = -1;
} else
{
if(mys.empty())
{
next[i] = -1;
continue;
}
Info * top = mys.top();
mys.pop();
if(top->c == '(')
{
int tmp = top->pos;
while((tmp-1>=0) && (next[tmp-1] != -1))
{
tmp = next[tmp-1];
}
next[i] = tmp;
ans = max(ans, i-next[i]+1);
} else
{
next[i] = -1;
while(!mys.empty())
mys.pop();
}
}
}
return ans;
}
};
int main() {
Solution *test = new Solution();
int data[] = {};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
printf("%d\n", test->longestValidParentheses(")()(())"));
return 0;
}
//
// Created by cms on 17/2/17.
//