Codeforces 519d A and B and interesting substrings (two-dimensional map + prefix and)

Source: Internet
Author: User

Link: http://codeforces.com/problemset/problem/519/D

Question:
The string S is composed of only lowercase letters '~ 'Z' all gives a value. The substring T must be equal to the start and end characters of T, and the value of the intermediate character is 0,
The number of substrings T.
Solution:
Set a two-dimensional map such as Map <LL, int> MP [26] to record the occurrence times of the prefix and value ending with each letter.
Then calculate the prefix and sum. Calculate the ANS before calculating sum + val [s [I, because sum [I-1]-sum [x] = 0 can indicate sum (I-1 ~ X + 1) This section is 0.

Code

1 # include <cstdio> 2 # include <iostream> 3 # include <algorithm> 4 # include <vector> 5 # include <string> 6 # include <string. h> 7 # include <cctype> 8 # include <math. h> 9 # include <stdlib. h> 10 # include <stack> 11 # include <queue> 12 # include <set> 13 # include <map> 14 # define LC (A) (a <1) 15 # define RC (A) (a <1 | 1) 16 # define mid (A, B) (a + B)> 1) 17 # define fin (name) freopen (name, "r", stdin) 18 # define fout (name) freopen (Na Me, "W", stdout) 19 # define CLR (ARR, Val) memset (ARR, Val, sizeof (ARR) 20 # DEFINE _ for (I, start, end) for (INT I = start; I <= end; I ++) 21 # define fast_io IOs: sync_with_stdio (false); cin. tie (0); 22 using namespace STD; 23 typedef long ll; 24 const int n = 1e5 + 5; 25 const ll inf64 = 1e18; 26 const int INF = 0x3f3f3f3f; 27 const double EPS = 1e-10; 28 29 int Val [30]; 30 Map <LL, int> MP [30]; 31 32 int main () {33 for (INT I = 0; I <26; I ++) {34 CIN> Val [I]; 35} 36 string STR; 37 CIN> STR; 38 ll sum = 0, ANS = 0; 39 For (INT I = 0; I <Str. length (); I ++) {40 int CH = STR [I]-'A'; 41 // calculate ans first, sum + = Val [CH] 42 // because sum [I-1]-sum [x] = 0 can indicate sum (I-1 ~ X + 1) This section is 0 43 ans + = MP [CH] [Sum]; 44 sum + = Val [CH]; 45 MP [CH] [Sum] ++; 46} 47 cout <ans <Endl; 48 return 0; 49}

 

Codeforces 519d A and B and interesting substrings (two-dimensional map + prefix and)

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.