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 ...
随机推荐
- git log 查看提交记录,参数:
git log 查看提交记录,参数:-n (n是一个正整数),查看最近n次的提交信息 $ git log -2 查看最近2次的提交历史记录 -- fileName fileName为任意文件名,查看指 ...
- P4147 玉蟾宫
P4147 玉蟾宫 给定一个 \(N * M\) 的矩阵 求最大的全为 \(F\) 的子矩阵 Solution 悬线法 限制条件为转移来的和现在的都为 \(F\) Code #include<i ...
- ELK 5.6.8 安装部署
操作系统版本: LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64: ...
- dubbo序列化
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. dubbo 支持多种序列化方式并且序列化是和协议相对应的.比如:dubbo协议的 dub ...
- 【1】【JUC】JDK1.8源码分析之ArrayBlockingQueue,LinkedBlockingQueue
概要: ArrayBlockingQueue的内部是通过一个可重入锁ReentrantLock和两个Condition条件对象来实现阻塞 注意这两个Condition即ReentrantLock的Co ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- baiduTemplate.js 百度JS模板引擎
baiduTemplate希望创造一个用户觉得“简单好用”的JS模板引擎 先展示两个例子,然后说说对baidutemplate.js的理解,从而将这一工具加到个人百宝箱里. <script id ...
- requsets模块和beautifulsoup模块
2.requests模块方法 requests是基于Python开发的HTTP库,使用Requests可以轻而易举的完成浏览器可有的任何操作. request.get() request.post() ...
- 3D点云数据分析:pointNet++论文分析及阅读笔记
PointNet的缺点: PointNet不捕获由度量空间点引起的局部结构,限制了它识别细粒度图案和泛化到复杂场景的能力. 利用度量空间距离,我们的网络能够通过增加上下文尺度来学习局部特征. 点集通常 ...
- 代码控制打电话、发短信、发邮件、打开手机app等操作
很多时候我们需要利用我门自己的app进行一些打电话.发短信等的操作,那么如何利用代码实现呢,下面就介绍一些简单的方法来实现这些操作. 一.打电话: <1>最简单.最直接的方法----直接跳 ...