cf519D . A and B and Interesting Substrings 数据结构map
题意:
已知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的更多相关文章
- [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...
- cf519D. A and B and Interesting Substrings(前缀和)
题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...
- 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 ...
- 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 [dp 前缀和 ]
传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...
- Erlang 的新数据结构 map 浅析
更新:文中示例代码直接从Joe的新版 Erlang 书中摘抄而来,其中模式匹配的代码有错误,现已纠正.应该用 := 匹配字段,而不是 => . 即将发布的 Erlang 17 最大变化之一包括新 ...
- ES6——数据结构 Map
数据结构 Map 字典: 用来存储不重复key的 Hash结构.不同于集合(Set)的是,字典使用的是 [键,值] 的形式来存储数据的. JavaScript 的对应那个(Object:{}) 只能用 ...
- ES6__数据结构 Map
/* 数据结构 Map */ /* * 字典:是用来存储不重复的key的hash结构.不同于集合(Set)的是,字典使用的是[键,值]的形式来储存数据的. *javaScript 的对象(Object ...
- 重学ES系列之新型数据结构Map应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- P235 实战练习(集合类2)、摇奖程序和验证码(修改版)
1.分别向Set集合以及List集合中添加“A”.“a”.“c”.“C”.“a”5个元素,观察重复值“a”能否在List集合以及Set集合中成功添加. package org.hanqi.practi ...
- GDB调试器
/*this project used for gdb debug c programs*//*At first,using compile command turn out the executab ...
- js动态生成数据列表
我们通常会使用table标签来展示数据内容,由于需要展示的数据内容是随时更换的,所以不可能将展示的数据列表写死在html写死在页面中,而是需要我们根据后台传来的数据随时更换,这个时候就需要我们使用js ...
- Python-requests之POST Data的json问题
代码如下: import json import requests r = requests.post(url, data = {"a": json.dumps({"b& ...
- 解决windows系统80端口被占用问题
在windows下部署web应用(80端口),启动时提示bind 80端口失败 检查端口占用: netstat -ano | findstr 发现System进程 (pid=4) 占用了端口 然而本机 ...
- linux小包集合
mingetty包 getty是Unix类操作系统启动时必须的三个步骤之一,用来开启终端,进行终端的初始化.目前一般常用的getty程序有如下几种:1.agetty(有时直接称为getty):容易设置 ...
- windows 2003 server 远程桌面禁用本地资源,磁盘驱动器,串行口,复制文件
首先进入组策略编辑器(开始-运行-gpedit.msc) 不要让用户在远端桌面和本地直接拷贝文件在远端桌面上进入它的组策略编辑器在 计算机配置->管理模板->Windows组件->终 ...
- 用HTML5构建一个流程图绘制工具
在我们的开发工程中经常会使用到各种图,所谓的图就是由节点和节点之间的连接所形成的系统,数学上专门有一个分支叫图论(Graph Theroy).利用图我们可以做很多工具,比如思维导图,流程图,状态机,组 ...
- 【redis】 linux 下redis 集群环境搭建
Redis集群 (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下) 127.0.0.1:63791 ...
- VBA 插入一行保留样式
Rows(processingRow).Insert ' 在指定的行数processingRow处插入一行 Rows(processingRow - 1).Select ' 选择上一行的整行 Sele ...