#include<bits/stdc++.h>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)
#define MOD 1000000007
#define MX 100010
long long cs[MX], val[30];
map< pair<long long, char>, long long> already;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int tc,kk=1, n, sl;
string s;
for(int i=0;i<26;i++)
cin>>val[i];
cin>>s;
sl=s.size();
cs[0]=val[ s[0]-'a'];
for(int i=1;i<sl;i++)
cs[i]=cs[i-1]+val[s[i]-'a'];
already[make_pair(cs[0], s[0] )]++;
long long ans=0;
for(int i=1;i<sl;i++)
{
ans+=already[make_pair(cs[i-1], s[i])];
already[ make_pair(cs[i], s[i])]++;
}
cout<<ans;
return 0;
}
Can you please explain the code a bit? Thanks!
ReplyDelete