cf519D. A and B and Interesting Substrings(前缀和)
题意
给出$26$个字母对应的权值和一个字符串
问满足以下条件的子串有多少
- 首尾字母相同
- 中间字母权值相加为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(前缀和)的更多相关文章
- cf519D . A and B and Interesting Substrings 数据结构map
题意: 已知26个小写字母有各自的权值(正,负,或0) 现在给出一个字符串,长度<=1e5 问这个字符串有多少个子串满足: 开头的字母和结尾的字母一样 字符串除了开头和结尾的字母外,其余的字母的 ...
- 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 ...
- [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...
- CF519 ABCD D. A and B and Interesting Substrings(map,好题)
A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...
- 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 ...
- Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)
题目链接:http://codeforces.com/problemset/problem/519/D 题目大意:给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值.求子串t满足t的 ...
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
题意: 对于26个字母 每个字母分别有一个权值 给出一个字符串,找出有多少个满足条件的子串, 条件:1.第一个字母和最后一个相同,2.除了第一个和最后一个字母外,其他的权和为0 思路: 预处理出sum ...
- Interesting (manacher + 前缀和处理)
题意:相邻的两端回文串的价值为两个回文串总的区间左端点 × 区间右端点.然后计算目标串中所有该情况的总和. 思路:首先用manacher求出所有中心点的最大半径,然后我们知道对于左区间我们把贡献记录在 ...
- CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)
题意:给出26个字母每个字母的价值,问字符串中有多少个满足以下条件的子串: 1.子串的第一个和最后一个相同 2.子串除了头和尾的其他字符的价值加起来和尾0 这题普通方法应该是O(n^2),但是在1e5 ...
随机推荐
- python list列表sort、sorted、reverse排序
列表排序:sort是修改原列表,sorted提供原列表的一个有序副本 li=[2,1,4,5,0]li.sort() #默认从小到大print li结果:[0, 1, 2, 4, 5] li=[2,1 ...
- selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。
转自:https://blog.csdn.net/xxj19950917/article/details/73002046
- ACM-ICPC2018北京网络赛 80 Days(双端队列+尺取)
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
- 存储过程接收JSON格式数据
前端有可能一次性上传多笔记录,并使用JSON序列化. 现在在MS SQL Server 2016版本上,可以直接处理JSO数据. 如下面的前端序列化的数据: DECLARE @json_string ...
- 如何使用jmeter连接数据库并提取数据库中的值作为参数,与响应信息中提取的值进行比较
思路: 连接数据库 获取数据库返回的数据 获取接口返回的数据 两者返回数据进行对比验证 连接数据库 1.jmeter要链接mysql数据库,首先得下载mysql jdbc驱动包(注:驱动包的版本一定要 ...
- CentOS 安装Perl环境
参考博文:https://blog.csdn.net/weixin_40192129/article/details/78610974 vmware tools需要perl环境的支持 安装perl支持 ...
- Unity 5 全局光照GI与新的烘焙系统初探
http://blog.csdn.net/sparrowfc/article/details/45650013 GI是啥 Realtime GI,实时全局光照,听上去就是一个非常高大上的词,但是越高大 ...
- Python 获取脚本路径以及脚本所在文件夹路径
import os script_path = os.path.realpath(__file__) script_dir = os.path.dirname(script_path)
- cogs1612. 大话西游
1612. 大话西游 http://www.cogs.pro/cogs/problem/problem.php?pid=1612 ★★ 输入文件:westward.in 输出文件:westwa ...
- VRTK3.3.0-003发出一条简单射线和监听
1丶在Right下继续添加脚本VRTK_Pointer和VRTK_StraightPointerRenderer 运行后默认是按住圆盘键出现射线,松开消失,大家可以自定义 2丶射线的监听事件 (1)在 ...