-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlong.cpp
More file actions
87 lines (78 loc) · 1.41 KB
/
Copy pathlong.cpp
File metadata and controls
87 lines (78 loc) · 1.41 KB
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
#include <bits/stdc++.h>
#include <vector>
#include <string>
using namespace std;
int longestValidParentheses(string s)
{
// if (argc < 2)
// {
// cout << "Give a sequence of paranthesses!\n"
// << endl;
// return 1;
// }
// auto s = string(argv[1]);
// int matched_length = 0;
stack<char> s1;
vector<int> v;
v.push_back(0);
int i = 0;
while (s[i] != 0)
{
if (s[i] == '(')
break;
i++;
}
while (s[i] != 0)
{
if (s[i] == '(')
s1.push(s[i]);
else if (s1.size() != 0)
{
s1.pop();
}
// if (v.back() != 0 || s[i] != ')')
v.push_back(s1.size());
i++;
}
int max = 0;
int curr_val = 0;
int c = 0;
vector<int> u;
u.push_back(0);
for (size_t i = 1; i < v.size() - 1; i++)
{ if(v[i]==v[i+1])
{
u.clear();
curr_val=-1;
continue;
}
if (v[i] > v[i - 1] && v[i] > v[i + 1])
{
u.back() += 2;
continue;
}
if (v[i] == curr_val)
{
if (u.back() > max)
max = u.back();
if (v[i] > v[i + 1])
{
int temp=u.back()+2;
u.pop_back();
u.back()+=temp;
curr_val--;
}
continue;
}
curr_val++;
u.push_back(0);
}
if (u.back() > max)
max = u.back();
// for (size_t i = 0; i < v.size(); i++)
// {
// cout << v[i] << endl;
// }
//cout << s << " " << max << endl;
return max;
}