Sunday, 1 March 2015

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

Problem: A and B and Interesting Substrings

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

long long cs[MX], val[30];
map< pair<long long, char>, long long> already;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, n, sl;
    string s;
    for(int i=0;i<26;i++)
        cin>>val[i];
    cin>>s;
    sl=s.size();
    cs[0]=val[ s[0]-'a'];
    for(int i=1;i<sl;i++)
        cs[i]=cs[i-1]+val[s[i]-'a'];
    already[make_pair(cs[0], s[0] )]++;
    long long ans=0;
    for(int i=1;i<sl;i++)
    {
        //cout<<cs[i]<<" "<<s[i]<<" "<<already[make_pair(cs[i], s[i])]<<endl;
        ans+=already[make_pair(cs[i-1], s[i])];
        already[ make_pair(cs[i], s[i])]++;
    }
    cout<<ans;

    return 0;
}

1 comment:

  1. Can you please explain the code a bit? Thanks!

    ReplyDelete