噢莫加纳加加加 • 2天前
#include <bits/stdc++.h>
using namespace std;
stack<int>s;
int main() {
string str;
cin >> str;
int maxlen = 0, start = 0;
s.push(-1);
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(' || str[i] == '[')
s.push(i);
else {
if (s.size() == 1) {
s.pop();
s.push(i);
} else {
char top = str[s.top()];
s.pop();
if (str[i] == ')' && top == '(' || str[i] == ']' && top == '[') {
int len = i - s.top();
if (maxlen < len) {
maxlen = len;
start = s.top() + 1;
}
} else {
s.pop();
s.push(i);
}
}
}
}
cout << str.substr(start, maxlen);
return 0;
}
评论:
请先登录,才能进行评论