Java 解题方案:子级 RBS 简单版 | AI生成和翻译

Home 2026.01

Question: 请提供 Codeforces 题目 B1. Sub-RBS (Easy Version) 的 Java 版本解决方案。

Answer:

这里是 Codeforces 题目 B1 - Sub-RBS (Easy Version) 的一个简洁的 Java 实现:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.util.in);
        int t = sc.nextInt();
        
        for (int test = 0; test < t; test++) {
            int n = sc.nextInt();
            String s = sc.next();
            
            // prefix[i] = balance after first i characters (0-based)
            int[] prefix = new int[n + 1];
            for (int i = 0; i < n; i++) {
                prefix[i + 1] = prefix[i] + (s.charAt(i) == '(' ? 1 : -1);
            }
            
            int ans = -1;
            
            // Try each possible first differing position i (0-based)
            // where s[i] == ')' and we pretend it is '(' instead
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) != ')') continue;
                
                // balance just before i
                int balBefore = prefix[i];
                
                // if we put '(' instead of ')' at position i
                int balAfterChange = balBefore + 1;
                
                // prefix must stay non-negative
                if (balAfterChange < 0) continue;
                
                // now try to take as many characters as possible from i+1 onwards
                int current = balAfterChange;
                int length = i + 1;  // 0..i inclusive
                
                boolean canContinue = true;
                for (int j = i + 1; j < n; j++) {
                    current += (s.charAt(j) == '(' ? 1 : -1);
                    if (current < 0) {
                        canContinue = false;
                        break;
                    }
                    length++;
                }
                
                // must end at balance 0
                if (canContinue && current == 0) {
                    ans = Math.max(ans, length);
                }
            }
            
            System.out.println(ans);
        }
        
        sc.close();
    }
}

Explanation

该解决方案采用了针对 Easy Version 的标准 Greedy 方法:

Time complexity: 最坏情况下为 O(t × n²),但由于 ∑n ≤ 2×10⁵ 且内层循环在实践中会提前 break,因此可以顺利在时间限制内通过。

References:


Back

x-ai/grok-4.1-fast

Donate