题面:
题意:
统计子串
s
[
i
.
.
.
j
]
s[i...j]
s[i...j] ,满足
s
[
i
]
=
=
s
[
j
]
s[i]==s[j]
s[i]==s[j] 且 (
s
u
m
[
j
−
1
]
−
s
u
m
[
i
+
1
]
=
=
0
sum[j-1]-sum[i+1] == 0
sum[j−1]−sum[i+1]==0 或
j
=
=
i
+
1
j==i+1
j==i+1) 的个数。(简略)
思路:
处理出前缀和sum,遍历字符串,对于当前字母
c
c
c 对答案的贡献是
s
u
m
[
i
−
1
]
sum[i-1]
sum[i−1] 出现过的次数。(感性理解一下?)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn];
ll sm[maxn];
char ss[maxn];
map<ll,ll>mp[26];
int main() {
for(int i = 0;i < 26; ++i){
scanf("%d",&a[i]);
mp[i].clear();
}
scanf("\n%s",ss + 1);
int n = strlen(ss + 1);
sm[0] = 0;
for(int i = 1;i <= n; ++i){
sm[i] = sm[i - 1] + a[ss[i] - 'a'];
}
ll ans = 0;
for(int i = 1;i <= n; ++i){
ans += mp[ss[i] - 'a'][sm[i - 1]];
mp[ss[i] - 'a'][sm[i]]++;
}
cout<<ans<<endl;
return 0;
}