forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecial Substrings.cpp
More file actions
44 lines (39 loc) · 859 Bytes
/
Special Substrings.cpp
File metadata and controls
44 lines (39 loc) · 859 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+1;
int N, M;
char S[maxN];
bool exists[26];
map<char,int> ch;
map<vector<int>,ll> dp;
bool containsEach(vector<int> V){
for(int i = 0; i < (int) V.size(); i++)
if(V[i] <= 0)
return false;
return true;
}
int main(){
scanf(" %s", S);
N = (int) strlen(S);
for(int i = 0; i < N; i++){
int c = (int) (S[i] - 'a');
if(!exists[c]){
ch[S[i]] = M++;
exists[c] = true;
}
}
ll ans = 0;
vector<int> freq(M);
dp[freq]++;
for(int i = 0; i < N; i++){
int c = ch[S[i]];
freq[c]++;
if(containsEach(freq))
for(int j = 0; j < M; j++)
freq[j]--;
ans += dp[freq];
dp[freq]++;
}
printf("%lld\n", ans);
}