Problem Statement
B ParanoidString
01 可以替换为 1,10 可以替换为 0。给定一个二进制串,问经过若干次替换后长度是否能变为 1。
Solution
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.util.Scanner;
public class ParanoidString {
public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { new Solver().solve(scanner); } scanner.close(); }
public static class Solver { public void solve(Scanner scanner) { long n = scanner.nextLong(); String str = scanner.next(); char[] s = str.toCharArray(); long ans = n * (n + 1) / 2; for (int i = 1; i < n; i++) { if (s[i] == '0' && s[i - 1] == '0') { ans -= i; } if (s[i] == '1' && s[i - 1] == '1') { ans -= i; } } System.out.println(ans); } } }
|