-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sum_of_Modes.cpp
58 lines (48 loc) · 1.18 KB
/
Sum_of_Modes.cpp
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
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
long long sumOfModes(const string &S)
{
int N = S.length();
vector<int> prefix_sum(N + 1, 0);
unordered_map<int, int> balance_map;
long long totalSum = 0;
// Precompute the prefix sums
for (int i = 1; i <= N; ++i)
{
prefix_sum[i] = prefix_sum[i - 1] + (S[i - 1] == '1' ? 1 : -1);
}
// Traverse through all prefix sums
for (int i = 0; i <= N; ++i)
{
int balance = prefix_sum[i];
// Add the current balance's count to the total sum
totalSum += balance_map[balance];
// If this balance has been seen before, it means there are substrings with this balance
balance_map[balance]++;
// If balance is 0, we have an equal number of '0's and '1's from the start
if (balance == 0)
{
totalSum += 1;
}
}
return totalSum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--)
{
int N;
cin >> N;
string S;
cin >> S;
cout << sumOfModes(S) << "\n";
}
return 0;
}