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 ...
随机推荐
- java编程之:生成rsa密钥
通过openssl工具生成RSA的公钥和私钥(opnssl工具可在互联网中下载到,也可以点此下载无线接口包,里面包含此工具) 打开openssl文件夹下的bin文件夹,执行openssl.exe文件: ...
- 恢复Ext3下被删除的文件(转)
恢复Ext3下被删除的文件(转) 前言 下面是这个教程将教你如何在Ext3的文件系统中恢复被rm掉的文件. 删除文件 假设我们有一个文件名叫 ‘test.txt’ $ls -il test.txt 1 ...
- stl迭代器原理
具体实现肯定不如书上讲的清楚了,这里只是根据侯捷书上的讲解,自己建立一条思路以及形成一些相关的概念 迭代器也可被称作智能指针,用于遍历容器内的元素,stl每个容器都实现了自己的iterator,ite ...
- linux mount (挂载命令)详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...
- org.springframework.web.filter.CharacterEncodingFilter
感谢:http://blog.csdn.net/heidan2006/article/details/3075730 很简单很实用的一个过滤器,当前台JSP页面和JAVA代码中使用了不同的字符集进行编 ...
- 【转】Android Drawable Resource学习(十一)、RotateDrawable
对另一个drawable资源,基于当前的level,进行旋转的drawable. 文件位置: res/drawable/filename.xml文件名即资源名 编译数据类型: 指向 RotateDra ...
- javascript中href和replace比较
在使用javascript的时候,有时候对于经常使用的方法太熟悉而忽略了他们之间原理的细微差别.举例如下:window.location.href,window.location.replace. ...
- python函数: 内置函数
forthttp://blog.csdn.net/pipisorry/article/details/44755423 Python内置函数 Python内置(built-in)函数随着python解 ...
- javascript 毫秒转日期 日期时间转毫秒
js毫秒时间转换成日期时间 var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数 大多数是用毫秒数除 ...
- spring的ResultSetWrappingSqlRowSet使用rs.getTimestamp取oracle数据库时分秒问题
oracle 11G数据库使用ojdbc14.jar驱动 rs.getTimestamp取不出时分秒问题: ResultSetWrappingSqlRowSet rs = processDao.que ...