Codeforces Round #294 (Div. 2)-D. A and B and Interesting Substrings

来源:互联网 发布:孤岛惊魂3 优化 编辑:程序博客网 时间:2024/06/06 08:44

原题链接

D. A and B and Interesting Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A and B are preparing themselves for programming contests.

After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.

A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).

B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).

Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.

Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?

Input

The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively.

The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.

Output

Print the answer to the problem.

Examples
input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1xabcab
output
2
input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1aaa
output
2
Note

In the first sample test strings satisfying the condition above are abca and bcab.

In the second sample test strings satisfying the condition above are two occurences of aa.


遍历字符串的每一个位置,计算前缀和.定义map<ll, int> m[26], m[i][j]表示以字符'a'+i为结尾的字符串的前缀和为j的情况有多少个.

遍历每一个字符str[i], 计算前缀和为sum, 那么以str[i]为结尾的满足条件的字符串个数为m[str[i]-'a'][sum-该字符的number]

#include <bits/stdc++.h>#define maxn 100005using namespace std;typedef long long ll;char str[maxn];int num[30];map<ll, int> m[26];int main(){//freopen("in.txt", "r", stdin);for(int i = 0; i < 26; i++) scanf("%d", num+i);scanf("%s", str);ll sum = 0, ans = 0;for(int i = 0; str[i]; i++){ans += m[str[i]-'a'][sum];sum += num[str[i]-'a'];m[str[i]-'a'][sum]++;}printf("%I64d\n", ans);return 0;}

0 0
原创粉丝点击