题意
给出$26$个字母对应的权值和一个字符串
问满足以下条件的子串有多少
- 首尾字母相同
- 中间字母权值相加为0
Sol
我们要找到区间满足$sum[i] - sum[j] = 0$
$sum[i] = sum[j]$
开$26$个map维护一下$sum$相等的子串就可以
/* */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 1e6 + , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int val[];
char s[MAXN];
map<int, int> mp[];
main() {
for(int i = ; i <= ; i++) val[i] = read();
scanf("%s", s + );
int N = strlen(s + ), ans = , sum = ;
for(int i = ; i <= N; i++) {
int x = s[i] - 'a' + ;
ans += mp[x][sum];
sum += val[x];
mp[x][sum]++;
}
printf("%I64d", ans);
return ;
}
/* */