-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodSubArrays.java
More file actions
47 lines (39 loc) · 1.55 KB
/
Copy pathGoodSubArrays.java
File metadata and controls
47 lines (39 loc) · 1.55 KB
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
37
38
39
40
41
42
43
44
45
46
47
import java.io.*;
import java.util.*;
public class GoodSubArrays { // start class
//_______________________________main method__________________________________
public static void main(String args[]) throws IOException { // start method
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(fin.readLine());
for (int t = 0; t < T; t++) {
int N = Integer.parseInt(fin.readLine());
String s = fin.readLine();
int[] arr = new int[N], preSum = new int[N + 1]; // prefix sum % N
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(s.substring(i, i+1));
preSum[i + 1] = preSum[i] + arr[i];
}
int[] count = new int[N+1];
for (int i = 0; i < N; i++) {
// System.out.println(preSum[i+1]);
int j = preSum[i+1]-i;
if (0 <= j && j <= N) count[j]++;
}
long ans = count[1];
for (int i = 0; i <= N; i++) {
ans += (long) count[i] * (count[i] - 1) / 2;
}
System.out.println(ans);
for (int i = 1; i <= N; i++) {
System.out.print(preSum[i] + " ");
}System.out.println();
for (int i = 0; i < N; i++) {
System.out.print(preSum[i+1]-i + " ");
}
System.out.println();
for (int i = 1; i <= N; i++) {
System.out.print(count[i] + " ");
}
}
}
}