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

Source: Internet
Author: User

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

 

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 when 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 stringS. Now they are trying to find out how many substringsTOf a stringSAre interesting to B (that is,TStarts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by ), before t for the first and the last one is equal to zero.

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

Input

The first line contains 26 integersXA,?XB,?...,?XZ(? -? 105? ≤?XI? ≤? (105)-the value assigned to lettersA,?B,?C,?...,?ZRespectively.

The second line contains stringSOf 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.

Sample test (s) 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 abve areAbcaAndBcab.

In the second sample test strings satisfying the condition abve are two occurencesAa.


 

 

 

 

 

Idea: first give the weight of 26 letters, then give a string, and then find out how many such substrings are there, the weight that satisfies the same header and tail and the value of the intermediate character is 0. (yesterday we did this and found that A was A dumb cha, speechless, then I was so excited that I didn't understand what it meant here.-> and also the sum of values of all letters (assigned by A), should t for the first and the last one is equal to zero .)

To calculate the value of 0 in the middle, you only need to calculate the prefix and the sum. Use map to implement it. Pay attention to the use of long.

 

 

AC code:

 

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     #include
     #define LL long long using namespace std;int val[30];char a[100005];map
      
        h[26];int main() {for(int i = 0; i < 26; i++) scanf("%d", &val[i]);scanf("%s", a);int len = strlen(a);LL ans = 0, sum = 0;for(int i = 0; i < len; i++) {ans += h[a[i] - 'a'][sum];sum += val[a[i] - 'a'];h[a[i] - 'a'][sum]++;}cout << ans << endl;return 0;}
      
    
   
  
 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.