题目链接:http://codeforces.com/problemset/problem/519/D

题目大意:
给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值。求子串t满足t的开头和结尾字符相同,且中间的字符的值相加为0,
求子串t的数目。
解题思路:
设置map<LL,int>mp[26]这样的二维map,记录对应以每个字母结尾的前缀和的值的出现次数。
然后计算前缀和sum,每次计算sum+val[s[i]]前,先计算ans,因为sum[i-1]-sum[x]==0才能说明sum(i-1~x+1)这一段为0 。

代码

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<cctype>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=1e5+;
const LL INF64=1e18;
const int INF=0x3f3f3f3f;
const double eps=1e-; int val[];
map<LL,int>mp[]; int main(){
for(int i=;i<;i++){
cin>>val[i];
}
string str;
cin>>str;
LL sum=,ans=;
for(int i=;i<str.length();i++){
int ch=str[i]-'a';
//注意先计算ans, 再sum+=val[ch]
//因为sum[i-1]-sum[x]==0才能说明sum(i-1~x+1)这一段为0
ans+=mp[ch][sum];
sum+=val[ch];
mp[ch][sum]++;
}
cout<<ans<<endl;
return ;
}

Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)的更多相关文章

  1. CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)

    题意:给出26个字母每个字母的价值,问字符串中有多少个满足以下条件的子串: 1.子串的第一个和最后一个相同 2.子串除了头和尾的其他字符的价值加起来和尾0 这题普通方法应该是O(n^2),但是在1e5 ...

  2. Codeforces 453E - Little Pony and Lord Tirek(二维线段树+ODT)

    Codeforces 题目传送门 & 洛谷题目传送门 一道难度 *3100 的 DS,而且被我自己搞出来了! 不过我终究还是技不如人,因为这是一个 \(n\log^2n\) + 大常数的辣鸡做 ...

  3. 【CodeForces】983 E. NN country 树上倍增+二维数点

    [题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...

  4. 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表

    [题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...

  5. 【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)

    [链接]我是链接 [题意] 接上一篇文章 [题解] 接(点我进入)上一篇文章. 这里讲一种用类似二维线段树的方法求矩形区域内点的个数的方法. 我们可以把n个正方形用n棵线段树来维护. 第i棵线段树维护 ...

  6. Codeforces Round #192 (Div. 2) A. Cakeminator【二维字符数组/吃掉cake,并且是一行或者一列下去,但是该行/列必须没有草莓的存在】

    A. Cakeminator time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. 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 megaby ...

  8. Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]

    传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...

  9. CF519 ABCD D. A and B and Interesting Substrings(map,好题)

    A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...

随机推荐

  1. bind&currying

    1. bind 基本用法 bind()是ECMAScript5中新增的方法,这个方法主要作用是将函数绑定至某个对象.当在函数f()上调用bind()方法并传入一个对象o作为参数,这个方法将返回一个新函 ...

  2. 什么是cap

    cap理论是分布式系统中非常重要的一个理念 什么是cap理论: Consistency一致性 Availability可用性 Partition-tolerance分区容忍性 CP: 高一致性C和分区 ...

  3. Redis在Windows上使用和集群配置

    一.什么是Redis Redis是一个开源的,使用C语言编写的面向键值对类型的分布式Nosql数据库系统,功能类似Memcache,但比Memcache功能更丰富.官网地址:https://redis ...

  4. java字符串转义,把&lt;&gt;转换成<>等字符【原】

    java字符串转义,把<>转换成<>等字符 使用的是commons-lang3-3.4 中的StringEscapeUtils类 package test; import ja ...

  5. java.lang.UnsupportedClassVersionError: xxx/xxxClass : Unsupported major.minor version 51.0【转】

    以下小段参考自overflow: Unsupported major.minor version 52.0 [duplicate] 或csdn也有提及 : http://blog.csdn.net/p ...

  6. Study 4 —— 表单标签

    表单:用于采集浏览者的相关数据.表单标记<form></form>表单的基本语法格式如下: <form action="url" method=&qu ...

  7. PHP7 学习笔记(九)phpsize动态编译openssl扩展 (微信公众平台)

    先吐槽,微信公众平台授权出问题了,尽然访问不了 一.问题描述: 使用PHP中的库函数file_get_contents时出现Unable to find the wrapper "https ...

  8. Linux 创建 时间命名 文件

    创建以 时间 命名文件:: touch /logs/`date +%Y-%m-%d_%d_%H:%M`.log touch "$(date +%Y-%m-%d_%H:%M:%S.TXT)

  9. MySQL - 日常操作一 增删改查

    mysql 源码安装 创建 mysql  账户 组 groupadd mysql useradd mysql -g mysql -M -s /bin/false 解压缩源码安装 .tar.gz cd ...

  10. Linux监控工具Spotlight on Unix

    1.介绍 Spotlight on Unix是一款Linux系统运行状况的监控工具,可以安装在Windows下,监控Linux服务器的运行状况. 监控项目包括:CPU.内存.交换空间.虚拟内存等的使用 ...