#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
cin >> str;
vector<int> dp(str.size(), 0);
int Max = 0;
for(int i = 1; i < str.size(); i++){
if(str[i] == ')'){
if(str[i-1] == '('){
dp[i] = 2 + (i-2 >= 0 ? dp[i-2] : 0);
Max = max(Max, dp[i]);
}
else if(dp[i-1] != 0 && i-dp[i-1]-1 >= 0 && str[i-dp[i-1]-1] == '('){
dp[i] = 2 + dp[i-1] + (i-dp[i-1]-2 >= 0 ? dp[i-dp[i-1]-2] : 0);
Max = max(Max, dp[i]);
}
}
}
cout << Max << endl;
return 0;
} #include <iostream> (720)#include <string> #include <stack> (850)#include <vector> using namespace std;int main() { string str; cin >> str; int n = str.length(); int index = 0; int m = 0; vector<int> dp(n,0); for (int i = 0;i < n;i++){ if (str[i] == ')'){ index = i - dp[i - 1] - 1; if ((index >= 0)&&(str[index] == '(')){ dp[i] = dp[i - 1] + 2 +(index > 0 ? dp[index - 1]:0); } } m = max(m,dp[i]); } cout << m << endl; return 0; }
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
int Max = 0;
vector<int>dp(str.size(),0);
for(int i = str.size()-2; i >= 0; i--)
{
if(str[i] == '(')
{
int j = i + 1 + dp[i+1];
if(j < str.size() && str[j] == ')')
{
dp[i] += dp[i+1] + 2;
if(j+1 < str.size())
dp[i] += dp[j+1];
}
if(Max < dp[i])
Max = dp[i];
}
}
cout<<Max<<endl;
return 0;
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] str = br.readLine().toCharArray();
int[] dp = new int[str.length];
int maxLen = 0;
for(int i = 1; i < str.length; i++){
if(str[i] == ')'){
int prev = i - dp[i - 1] - 1;
if(prev >= 0 && str[prev] == '(')
dp[i] = dp[i - 1] + 2 + (prev > 0? dp[prev - 1]: 0);
}
maxLen = Math.max(maxLen, dp[i]);
}
System.out.println(maxLen);
}
} #include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MAXLEN 100001
int main(void) {
int len, *dp, pre, res = 0;
char str[MAXLEN];
scanf("%s", str);
len = (int) strlen(str);
dp = (int *) malloc(sizeof(int) * len);
dp[0] = 0;
for (int i = 1; i < len; i++) {
if (str[i] == '(') {
dp[i] = 0;
continue;
}
pre = i - dp[i - 1] - 1;
if (pre >= 0 && str[pre] == '(') {
dp[i] = dp[i - 1] + 2 + (pre > 0 ? dp[pre - 1] : 0);
}
res = MAX(res, dp[i]);
}
printf("%d\n", res);
free(dp);
return 0;
} #include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
int longestValidParentheses(string s) {
vector<int> dp(s.size(),0);//dp[i]代表区间为[0,i](i是字符串下标)的最长有效长度
int ans = 0;
for(int i=0;i<s.size();i++){
if(s[i]=='(') dp[i] = 0;//最后一位是'(',说明这个子串有效长度是0
else if (i-1>=0&&s[i-1]=='(') //最后一位是")",需要分情况讨论倒数第二位,这里倒数第二位是左括号
dp[i] = 2+(i-2>=0?dp[i-2]:0);//字符串举例:"xxxx()"
else if(i-1>=0&&s[i-1]==')'){//倒数第二位是右括号
int prev_index = i-dp[i-1]-1;//与最后右括号匹配的左括号的下标索引,举例:(xxxx),中间xxxx合法字符串的有效长度是dp[i-1]
int prev_prev_index = i-dp[i-1]-2;//上面左括号匹配的左边子串下标,举例:()(xxxx)
if(prev_index>=0&&s[prev_index]=='(')
dp[i] = dp[i-1]+2+(prev_prev_index>=0?dp[prev_prev_index]:0);//把上面举例的两种情况合并
}
ans = max(ans,dp[i]);
}
return ans;
}
};
int main(){
string s;
cin>>s;
Solution so;
cout<<so.longestValidParentheses(s)<<endl;
return 0;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int length = str.length();
int left = 0;
int right = 0;
int size = 0;
for (int i = 0; i < length; i++) {
if (str.charAt(i) == '(') {
left ++;
} else {
right ++;
if (right == left) {
size = Math.max(2 * left, size);
} else if (right > left) {
left = 0;
right = 0;
}
}
}
System.out.println(size);
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s8=new Scanner(System.in);
String s=s8.nextLine();
LinkedList<Integer> s1=new LinkedList<>();
s1.push(-1);
int max=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==')'){
s1.pop();
if(s1.isEmpty()) s1.push(i);
else max=Math.max(max,i-s1.peek());
}
else s1.push(i);
}
System.out.println(max);
}
} import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main{
public static int MaxLength(String str){
if(str==null||str.equals("")){
return 0;
}
char[] chas=str.toCharArray();
int[] dp=new int[chas.length];
int pre=0;
int res=0;
for(int i=1;i<chas.length;i++){
if(chas[i]==')'){
pre=i-dp[i-1]-1;
if(pre>=0&&chas[pre]=='('){
dp[i]=dp[i-1]+2+(pre>0?dp[pre-1]:0);
}
}
res=Math.max(res,dp[i]);
}
return res;
}
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
System.out.print(MaxLength(str));
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(maxLength(s));
}
private static int maxLength(String s){
if(s==null||s.length()<=1){
return 0;
}
char[] chars = s.toCharArray();
int max=0;
int[] dp=new int[chars.length];
for(int i=1;i<chars.length;i++){
if(chars[i]==')'){
if(i-dp[i-1]-1>=0&&chars[i-dp[i-1]-1]=='('){
dp[i]=dp[i-1]+2+(i-dp[i-1]-1>0?dp[i-dp[i-1]-2]:0);//这一句代码分为3大部分:
max=Math.max(max,dp[i]);
}
}
}
return max;
}
} #include<iostream>
#include<string>
using namespace std;
class Solution{
public:
static int fun(string& s) {
int left = 0, right = 0, res = 0;
for (auto c : s) {
if (c == '(') {
++left;
}
else if (c == ')') {
++right;
if(right == left){
if(left > res) res = left;
}
else if(right > left){
left = right = 0;
}
}
else {//有'*'
left = right = 0;
}
}
left = right = 0;
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] == ')') {
++right;
}
else if (s[i] == '(') {
++left;
if(right == left){
if(right > res) res = left;
}
else if(left > right){
left = right = 0;
}
}
else {
left = right = 0;
}
}
return 2 * res;
}
};
int main(){
string s;
while(cin >> s){
cout << Solution::fun(s) << endl;
}
return 0;
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
String s = sc.nextLine();
Stack<Integer> stack = new Stack<>();
stack.push(-1);
int ret = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
stack.push(i);
}
if (c == ')') {
int pre = stack.pop();
if (stack.isEmpty()) {
stack.push(i);
} else {
if (stack.peek() >= 0 &&
isValid(s.substring(stack.peek(), i + 1))) {
ret = Math.max(ret, i - stack.peek());
} else {
if (isValid(s.substring(pre, i + 1))) {
ret = Math.max(ret, i - pre + 1);
}
}
}
}
}
System.out.println(ret);
}
}
private static boolean isValid(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '(' && s.charAt(i) != ')') {
return false;
}
}
return true;
}
}