题意:

已知26个小写字母有各自的权值(正,负,或0)

现在给出一个字符串,长度<=1e5

问这个字符串有多少个子串满足:

开头的字母和结尾的字母一样

字符串除了开头和结尾的字母外,其余的字母的权值和为0

本来是一道水题,一看就知道大体的思路了,结果硬是搞了一个多小时

先是用set,发现超时了,改为用map

思路:

1.我们先预处理这个字符串的权值的前缀和

sum[i]表示从字符串的起点到i的权值和

2.我们要找到的子串,设以l开头和r结尾,则有

str[r] == str[l] 和 sum[r-1] - sum[l] == 0

即:sum[r] - sum[l] == val[str[r] - 'a']

那么我们只要记录:

(char,sum)以0为开头,char为结尾,权值和为sum的子串的个数即可

实现:用一个map

遍历一遍字符串,对于每一个(char,sum),找到前面出现多少(char,sum-val[char-'a'])

然后更新res,并且map[(char,sum)]++

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <cstdlib> #define LL long long
#define fir first
#define sec second using namespace std; int val[];
LL sum[+];
char str[+]; map< pair<char,LL>,int > ms;
map< pair<char,LL>,int > :: iterator it; void solve()
{
for(int i=;i<;i++){
scanf("%d",&val[i]);
}
scanf("%s",str);
int len = strlen(str); ms.clear();
LL res = ;
sum[] = ; for(int i=;i<=len;i++){
sum[i] = sum[i-] + val[str[i-] - 'a'];
} pair<char,LL> pr;
LL tmp;
for(int i=;i<=len;i++){
pr = make_pair(str[i-],sum[i]);
it = ms.find(make_pair(pr.fir,pr.sec - val[pr.fir-'a']));
if(it != ms.end())
res += it->sec;
ms[pr]++;
} //printf("%lld\n",res);
printf("%I64d\n",res);
return ;
} int main()
{
solve();
return ;
}

cf519D . A and B and Interesting Substrings 数据结构map的更多相关文章

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

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

  2. cf519D. A and B and Interesting Substrings(前缀和)

    题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...

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

  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 [dp 前缀和 ]

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

  6. Erlang 的新数据结构 map 浅析

    更新:文中示例代码直接从Joe的新版 Erlang 书中摘抄而来,其中模式匹配的代码有错误,现已纠正.应该用 := 匹配字段,而不是 => . 即将发布的 Erlang 17 最大变化之一包括新 ...

  7. ES6——数据结构 Map

    数据结构 Map 字典: 用来存储不重复key的 Hash结构.不同于集合(Set)的是,字典使用的是 [键,值] 的形式来存储数据的. JavaScript 的对应那个(Object:{}) 只能用 ...

  8. ES6__数据结构 Map

    /* 数据结构 Map */ /* * 字典:是用来存储不重复的key的hash结构.不同于集合(Set)的是,字典使用的是[键,值]的形式来储存数据的. *javaScript 的对象(Object ...

  9. 重学ES系列之新型数据结构Map应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Hardly Hard

    You have been given the task of cutting out a quadrilateral slice of cake out of a larger, rectangul ...

  2. poj2762 强连通+拓扑序

    题意:有 n 个房间,不同房间之间有单向通道,问是否任意两个房间 A .B 都可以从 A 到 B 或从 B 到 A(有一条有就可以). 在这题中,如果一些点是在同一个强连通分量中,那么这些点肯定能够相 ...

  3. python study - 正则表达式

    第 7 章 正则表达式 7.1. 概览 7.2. 个案研究:街道地址 7.3. 个案研究:罗马字母 7.3.1. 校验千位数 7.3.2. 校验百位数 7.4. 使用 {n,m} 语法 7.4.1. ...

  4. Bootloader的原理以及实现(转载)

    BootLoader工作原理 BootLoader工作原理 BootLoader指系统启动后,在操作系统内核运行之前运行的一段小程序.通过BootLoader,我们可以初始化硬件设备.建立内存空间的映 ...

  5. 26 个 jQuery使用技巧

    1. 禁用右键点击(Disable right-click) $(document).ready(function(){ $(document).bind("contextmenu" ...

  6. springMvc源码学习之:spirngMvc获取请求参数的方法

    一.      通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMeth ...

  7. ROM、RAM、DRAM、SRAM和FLASH区别

    ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写. ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都 ...

  8. mount挂载错误

    错误信息: usa@usa-desktop:~/svn/aispeech/speechplatform/trunk/proxy/src$ sudo mount -t tmpfs -o size=200 ...

  9. python_Memcached

    一.Memcached 1.Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...

  10. 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)

    本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...