题意

给出$26$个字母对应的权值和一个字符串

问满足以下条件的子串有多少

  1. 首尾字母相同
  2. 中间字母权值相加为0

Sol

我们要找到区间满足$sum[i] - sum[j] = 0$

$sum[i] = sum[j]$

开$26$个map维护一下$sum$相等的子串就可以

/*

*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 1e6 + , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int val[];
char s[MAXN];
map<int, int> mp[];
main() {
for(int i = ; i <= ; i++) val[i] = read();
scanf("%s", s + );
int N = strlen(s + ), ans = , sum = ;
for(int i = ; i <= N; i++) {
int x = s[i] - 'a' + ;
ans += mp[x][sum];
sum += val[x];
mp[x][sum]++;
}
printf("%I64d", ans);
return ;
}
/* */

cf519D. A and B and Interesting Substrings(前缀和)的更多相关文章

  1. cf519D . A and B and Interesting Substrings 数据结构map

    题意: 已知26个小写字母有各自的权值(正,负,或0) 现在给出一个字符串,长度<=1e5 问这个字符串有多少个子串满足: 开头的字母和结尾的字母一样 字符串除了开头和结尾的字母外,其余的字母的 ...

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

  3. [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】

    题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...

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

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

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

  6. Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)

    题目链接:http://codeforces.com/problemset/problem/519/D 题目大意:给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值.求子串t满足t的 ...

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

    题意: 对于26个字母 每个字母分别有一个权值 给出一个字符串,找出有多少个满足条件的子串, 条件:1.第一个字母和最后一个相同,2.除了第一个和最后一个字母外,其他的权和为0 思路: 预处理出sum ...

  8. Interesting (manacher + 前缀和处理)

    题意:相邻的两端回文串的价值为两个回文串总的区间左端点 × 区间右端点.然后计算目标串中所有该情况的总和. 思路:首先用manacher求出所有中心点的最大半径,然后我们知道对于左区间我们把贡献记录在 ...

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

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

随机推荐

  1. HL7 Event Type

    Table 0003 - Event type Value Description A01 ADT/ACK - Admit / visit notification A02 ADT/ACK - Tra ...

  2. C# 使用 MemoryStream 将数据写入内存

    转自:http://blog.csdn.net/andrew_wx/article/details/6629951 常用的MemoryStream构造函数有以下3种. 1:MemoryStream() ...

  3. SendMail发送回执及读取收件箱

    一.SendMail发送有回执提示 1.邮件发送配置 Properties props = new Properties(); String smtp = "smtp.qq.com" ...

  4. hadoop学习路线(转)

    刚刚入门hadoop,如何去学习hadoop.google一篇学习路线图,与童鞋们共勉: 转自:http://blog.csdn.net/zhoudaxia/article/details/88017 ...

  5. switch()出现警告warning:enumeration value ‘xxx’ not handled in switch

    enumeration value ... not handled in switch 如果在switch中使用使用枚举类型,如:switch(枚举类型) 枚举类型的个数没有全部列出,就会报这个警告

  6. posix 正则库程序

    使用的是posix 正则库,参考: http://see.xidian.edu.cn/cpp/html/1428.html 执行匹配的时: gcc myreg.c ip.pat 内容: ip.*[0- ...

  7. 猜socklen_t的原型

    编写tcp时碰到这个类型,感觉他就是int型. 百度了一下: typedef int socklen_t typedef int ssize_t 我去/usr/include 下grep -r soc ...

  8. 多行文字超出字数部分省略(主要解决不兼容;display: -webkit-box;的浏览器)

    注明:内容来处https://www.cnblogs.com/ss977/p/5846176.html 1.现webkit内核的浏览器支持display: -webkit-box;属性, 所以网页中显 ...

  9. Unity 5.6中的混合光照(上)

    https://mp.weixin.qq.com/s/AbWM21sihHw5pFdMzENDPg 在Unity 5中,光照得到了很大的改进.现在,创建高度逼真的游戏已成为可能.但是,出于对性能的考虑 ...

  10. 以太坊开发教程(一) truffle框架简介/安装/使用

    通常一个DAPP的开发包括两部分:智能合约的开发和提供合约进行调用的前端页面. truffle提供了对这两部分内容比较简单的开发方式,特别是在开发/测试阶段.给开发人员提供快捷的打包/部署,已经本地服 ...