我测了很多组数据都没问题,为什么提交通过 0% 用例,很费解啊,有大佬分析下吗?
import java.util.*;
/**
* 11
* R))LL(((RR)
*
* 0 -1 -2 -2 -2 -1 2 -1 -1 -1 3
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
in.nextLine();
String s = in.nextLine();
StringBuilder sb = new StringBuilder();
int p = 0;
for(int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if(c == '(' || c == ')') {
sb.insert(p++, c);
}
else if(c == 'D') {
if(p == 0) {
}
else {
sb.deleteCharAt(--p);
}
}
else if(c == 'R') {
if(p == sb.length()) {
}
else
++p;
}
else if(c == 'L') {
if(p == 0) {
}
else
--p;
}
int ans = solve(sb);
System.out.print(ans);
if(i != s.length() - 1)
System.out.print(" ");
}
System.out.println();
}
}
public static int solve(StringBuilder sb) {
int n = sb.length();
int left = 0, right = 0;
boolean ok = true;
int ans = 0;
int max_dep = 0;
for(int i = 0; i < n; ++i) {
char c = sb.charAt(i);
if(c == '(')
++left;
else
++right;
if(left < right) {
ok = false;
ans = Math.max(ans, right - left);
}
else if(left == right) {
max_dep = Math.max(max_dep, left);
left = 0;
right = 0;
}
}
left = 0;
right = 0;
for(int i = n - 1; i >= 0; --i) {
char c = sb.charAt(i);
if(c == ')') {
++right;
}
else {
++left;
}
if(right < left) {
ok = false;
ans = Math.max(ans, left - right);
}
else if(left == right) {
max_dep = Math.max(max_dep, left);
left = 0;
right = 0;
}
}
if(ok) {
return max_dep;
}
else {
return -ans;
}
}
}
全部评论
(9) 回帖