-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathABC-String_Second_Solution.cpp
More file actions
45 lines (43 loc) · 955 Bytes
/
ABC-String_Second_Solution.cpp
File metadata and controls
45 lines (43 loc) · 955 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
#include<bits/stdc++.h>
using namespace std;
int prea[100000],preb[100000],prec[100000];
int main()
{
string s;
cin>>s;
long long n=s.size();
map<pair<int,int> , int> mp;
long long ans=0;
mp[{0,0}]=1;
for(int i=1;i<=n;i++)
{
if(s[i-1]=='A')
{
prea[i]=prea[i-1]+1;
preb[i]=preb[i-1];
prec[i]=prec[i-1];
}
else if(s[i-1]=='B')
{
prea[i]=prea[i-1];
preb[i]=preb[i-1]+1;
prec[i]=prec[i-1];
}
else
{
prea[i]=prea[i-1];
preb[i]=preb[i-1];
prec[i]=prec[i-1]+1;
}
pair<int,int> p=make_pair(prea[i]-preb[i] , prea[i]-prec[i] );
if(mp.find(p)!=mp.end())
{
ans+=mp[p];
mp[p]++;
}
else
mp[p]=1;
}
cout<<ans;
return 0;
}