Codeforces Round #294 Div2 D(A and B and Interesting Substrings)

Problem

给一个长度为 N 的字符串 S ,字符集是 [a,z] ,每个字符都有一个权值 Vi ,求有多少个子串 subS 满足以下条件:

1. |subS|>=2

2. subS[0]=subS[|subS|1]

3. |subS|2i=1Vi=0

Limits

TimeLimit(ms):2000

MemoryLimit(MB):256

N[1,105]

Vi[105,105]

Look up Original Problem From here

Solution

如果没有条件2,用前缀和思想可做。定义 map:m[i] 表示前缀和的权值为 i 的有多少个,从左往右扫 N 个前缀和,当前扫到的前缀和权值为 i ,则查询 m[i] ,那么就有 m[i] 个子串为 0 ,再更新 map

加上条件2,用26个map即可…

Complexity

TimeComplexity:O(N×log2N)

MemoryComplexity:O(N)

My Code

//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-9
#define MOD 1000000007
#define MAXN 2015
#define N 100100
#define M
map<ll,ll>countit[30];
map<ll,ll>::iterator it;
ll letterpoints[30],now,ans;
ll points(char c){
    return letterpoints[c-'a'];
}
int lens;
char s[N];
int main(){
    rep(i,0,26){
        scanf("%lld",letterpoints+i);
    }
    scanf("%s",s);
    lens=len(s);
    now=points(s[0]);
    ans=0;
    countit[s[0]-'a'][now]+=1LL;
    rep(i,1,lens){
        ans+=countit[s[i]-'a'][now];
        now+=points(s[i]);
        countit[s[i]-'a'][now]+=1LL;
    }
    cout<<ans<<endl;
}

你可能感兴趣的:(前缀和,map统计)