Created
August 16, 2020 10:12
-
-
Save yooniversal/57883cfcb2c563a8eb8087ef39663b70 to your computer and use it in GitHub Desktop.
C. Good Subarrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstring> | |
#include <cstdio> | |
#include <vector> | |
#include <map> | |
using namespace std; | |
#define INF 987654321 | |
#define MAXSIZE 100001 | |
typedef long long ll; | |
int main() { | |
cin.tie(nullptr); | |
cout.tie(NULL); | |
ios_base::sync_with_stdio(false); | |
int t; cin >> t; | |
while (t--) { | |
int n; string s; | |
cin >> n >> s; | |
map<int, int> key; | |
vector<int> pre(n + 1); | |
int res = 0; | |
pre[0] = 0; | |
for (int i = 0; i < n; i++) { | |
pre[i+1] += pre[i] + (s[i] - '0'); | |
} | |
for (int i = 1; i <= n; i++) { | |
key[pre[i-1] - i]++; | |
res += key[pre[i] - i - 1]; | |
} | |
cout << res << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment