Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)
题目链接: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+前缀和)的更多相关文章
- CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)
题意:给出26个字母每个字母的价值,问字符串中有多少个满足以下条件的子串: 1.子串的第一个和最后一个相同 2.子串除了头和尾的其他字符的价值加起来和尾0 这题普通方法应该是O(n^2),但是在1e5 ...
- Codeforces 453E - Little Pony and Lord Tirek(二维线段树+ODT)
Codeforces 题目传送门 & 洛谷题目传送门 一道难度 *3100 的 DS,而且被我自己搞出来了! 不过我终究还是技不如人,因为这是一个 \(n\log^2n\) + 大常数的辣鸡做 ...
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
- 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)
[链接]我是链接 [题意] 接上一篇文章 [题解] 接(点我进入)上一篇文章. 这里讲一种用类似二维线段树的方法求矩形区域内点的个数的方法. 我们可以把n个正方形用n棵线段树来维护. 第i棵线段树维护 ...
- 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 ...
- 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 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 ...
- CF519 ABCD D. A and B and Interesting Substrings(map,好题)
A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...
随机推荐
- emptyDir与hostPath
目录 Volume类型 emptyDir hostPath Volume类型 volume是kubernetes Pod中多个容器访问的共享目录.volume被定义在pod上,被这个pod的多个容器挂 ...
- 利用Screen重启DevStack服务
上篇介绍了DevStack如何安装部署.集成化工具有好处,但在系统重启后,OpenStack的各个服务并不会随系统重启而启动,需要利用screen来重启,接下来就记录下如何使用screen. 准备工作 ...
- secure CRT the remote system refused the connection 解决办法
1.安装ssh服务器和客户端 apt-get install openssh-server apt-get install openssh-client 2.重启ssh /etc/init.d/ssh ...
- PHP 进行支付宝开发中return_url和notify_url的区别分析
在支付宝处理业务中return_url,notify_url是返回些什么状态呢,我们要根据它来做一些处理就必须了解return_url,notify_url的区别,下面我就来给大家介绍; 一.问题描述 ...
- Winform中使用WPF控件并动态读取Xaml
1.添加新项 2.在构造函数中加入 public partial class UserControl1 : UserControl { public UserControl1() { Initiali ...
- 用Python中的re做信息筛选
背景 平时工作中,我们经常会处理大量的元数据(Raw Data),而一般的文件编辑器只能一次查询一个关键字,这就难以连续的分析元数据,比如分析产品日志文件(log),日志可能包括很多informati ...
- Spring第一个helloWorld
Spring 简介: 轻量级:Spring是非侵入性的-基于Spring开发的应用中的对象可以不依赖于Spring的API 依赖注入(DI—dependdency injection.IOC) 面向切 ...
- tf.matmul()和tf.multipy()的区别
首先我们分析一下下面的代码: import tensorflow as tf import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]]) ...
- luogu P1943 LocalMaxima_NOI导刊2009提高(1)
又是有关于\(1-n\)排列的题,考虑从大到小依次插入构造排列 对于第\(i\)个数(也就是\(n-i+1\)),只有当它插在当前排列最前面时才会使那个什么数的个数+1,而在最前面的概率为\(\fra ...
- 列式数据库~clickhouse 底层存储原理
简介:今天介绍列式数据库的一些基本原理 一 数据目录 Data目录 数据存储目录,数据按照part分成多个文件夹,每个文件夹下存储相应数据和对应的元信息文件 Metadata 表定义语句,存储所有表 ...