-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF_1398C.cpp
More file actions
54 lines (45 loc) · 829 Bytes
/
Copy pathCF_1398C.cpp
File metadata and controls
54 lines (45 loc) · 829 Bytes
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
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int arr[100001];
int prefixSum[100001];
int main() {
int t;
cin >> t;
while (t--)
{
int n;
string str;
cin >> n >> str;
for (int i = 0; i < n; i++)
{
arr[i] = str[i] - '0';
if (i - 1 < 0)
{
prefixSum[i] = arr[i];
}
else
{
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
}
// good => prefix[r] - prefix[l] == r - l
// prefix[r] - r == prefix[l] - l
map<int, long long> cnt;
cnt[0] = 1;
for (int i = 0; i < n; i++)
{
int x = prefixSum[i] - (i+1);
cnt[x]++;
}
long long result = 0;
// nC2
for (std::map<int, long long>::iterator it = cnt.begin(); it != cnt.end(); ++it)
{
result += (it->second) * ((it->second) - 1) / 2;
}
cout << result << endl;
}
}