Skip to content

Commit 98e4330

Browse files
committed
feat(codeforces): implement solution for p2190B1
1 parent bdd3ffa commit 98e4330

File tree

1 file changed

+36
-33
lines changed
  • src/main/java/com/lzw/solutions/codeforces/p2190B1

1 file changed

+36
-33
lines changed

src/main/java/com/lzw/solutions/codeforces/p2190B1/MainPro.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,60 @@
66
public class MainPro {
77
public static void main(String[] args) {
88
Scanner sc = new Scanner(System.in);
9-
int t = sc.nextInt();
10-
11-
for (int test = 0; test < t; test++) {
9+
int T = sc.nextInt();
10+
for (int cas = 0; cas < T; cas++) {
1211
int n = sc.nextInt();
1312
String s = sc.next();
1413

15-
// prefix[i] = balance after first i characters (0-based)
16-
int[] prefix = new int[n + 1];
17-
for (int i = 0; i < n; i++) {
18-
prefix[i + 1] = prefix[i] + (s.charAt(i) == '(' ? 1 : -1);
19-
}
20-
2114
int ans = -1;
2215

23-
// Try each possible first differing position i (0-based)
24-
// where s[i] == ')' and we pretend it is '(' instead
25-
for (int i = 0; i < n; i++) {
26-
if (s.charAt(i) != ')') continue;
16+
for (int split = 1; split <= n; split += 2) { // possible lengths of t
17+
// check if we can form a regular seq of length 'split' that is better than s
18+
// i.e. first difference position has ( in t, ) in s
2719

28-
// balance just before i
29-
int balBefore = prefix[i];
20+
int open = 0;
21+
boolean foundDiff = false;
22+
boolean ok = true;
3023

31-
// if we put '(' instead of ')' at position i
32-
int balAfterChange = balBefore + 1;
24+
for (int i = 0; i < split; i++) {
25+
char want = (open > 0 && i + 1 < split) ? ')' : '(';
26+
// but we prefer to follow s as long as possible
3327

34-
// prefix must stay non-negative
35-
if (balAfterChange < 0) continue;
36-
37-
// now try to take as many characters as possible from i+1 onwards
38-
int current = balAfterChange;
39-
int length = i + 1; // 0..i inclusive
28+
if (!foundDiff) {
29+
if (s.charAt(i) == '(') {
30+
open++;
31+
} else {
32+
// s has ), we can choose to put ( here → this becomes the diff point
33+
if (open > 0) {
34+
// we could close, but we decide to open instead
35+
foundDiff = true;
36+
open++; // pretend we put (
37+
} else {
38+
open--; // forced to follow s
39+
}
40+
}
41+
} else {
42+
// after difference, freely choose to make it valid
43+
if (open > 0 && i + 1 < split) {
44+
open--; // prefer close if possible
45+
} else {
46+
open++; // open
47+
}
48+
}
4049

41-
boolean canContinue = true;
42-
for (int j = i + 1; j < n; j++) {
43-
current += (s.charAt(j) == '(' ? 1 : -1);
44-
if (current < 0) {
45-
canContinue = false;
50+
if (open < 0) {
51+
ok = false;
4652
break;
4753
}
48-
length++;
4954
}
5055

51-
// must end at balance 0
52-
if (canContinue && current == 0) {
53-
ans = Math.max(ans, length);
56+
if (ok && open == 0 && foundDiff) {
57+
ans = Math.max(ans, split);
5458
}
5559
}
5660

5761
System.out.println(ans);
5862
}
59-
6063
sc.close();
6164
}
6265
}

0 commit comments

Comments
 (0)