题意:

已知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. Codeforces378 D Kostya the Sculptor(贪心)(逻辑)

    Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  2. phpwind8.7升级9.0.1过程(四)20130207升级到20141228

    每一次升级前都要注意备份 1.网站根目录的所有文件 2.网站的数据库 根据phpwind官方教程 更新到20130702版本成功并备份 更新到20140428版本成功并备份 20141228版本的更新 ...

  3. Android——ListView相关作业(修改版)

    给GridView提供点击按钮添加新数据,单击项目修改,长按删除功能 activity_practise7的layout文件: <?xml version="1.0" enc ...

  4. MySql的FIND_IN_SET()查询函数的使用

    表 table的结构如下: id title 1 12,21 2 21,32 3 45,21,78 4 221,45,74 5 34,421,121 6 52,21,321 现在用sql语句查出来字段 ...

  5. Python小白好教程

    提供一些Python的基础教程. Crossin的编程教师:网址:http://crossincode.com/home/ 廖雪峰的官方网站 网址:http://www.liaoxuefeng.com ...

  6. Win10系统下安装Tensorflow

    TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,具备良好的灵活性和拓展性. 过去TensorFlow只支持Linux和Mac OS,而没有提供windows的支持. ...

  7. mysql join的方式结果集

    一张图说明  left join, right join, inner join, full outer join

  8. python sys.argv[]

    sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...

  9. 2016国赛B题小区数据爬取软件

    -------------------------请以任何方式留言给作者,否则视为窃取----------------------------- 看你们找数据找的那么辛苦 我就苦逼的花了1个小时写了个 ...

  10. 转载:scikit-learn学习之决策树算法

    版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] ================== ...