[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings
题目大意
给定26个小写字母的权值,一共26个整数(有正有负)。
给定一个小写字母组成的字符串(长度10^5),求有多少长度大于1的子串满足:
1)首尾字符相同。
2)除了首尾字符外,其他字符的权值和为0。
题目分析
使用STL Map。开26个Map,给每个字母开一个。
先求出权值的前缀和 Sum[] 。
然后1到l枚举每一位字符,如果是a,那么 Ans += Map[a][Sum[i - 1]];
然后 ++Map[a][Sum[i]];
这个原理是十分简单的,但是我就是这么弱,以至于比赛的时候想不到,然后用了一种复杂的方法,最后WA了= =
时间复杂度 O(n log n) 。
代码
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map> using namespace std; inline int gmin(int a, int b) {return a < b ? a : b;}
inline int gmax(int a, int b) {return a > b ? a : b;} typedef long long LL;
typedef unsigned long long ULL; const int MaxL = 100000 + 5, MaxC = 26 + 5; int l;
int V[MaxC], A[MaxL]; LL Ans;
LL Sum[MaxL]; char S[MaxL]; map<LL, int> Mp[MaxC]; int main()
{
for (int i = 0; i < 26; ++i) scanf("%d", &V[i]);
scanf("%s", S + 1);
l = strlen(S + 1);
for (int i = 1; i <= l; ++i) A[i] = S[i] - 'a';
Sum[0] = 0ll;
Ans = 0ll;
for (int i = 1; i <= l; ++i) Sum[i] = Sum[i - 1] + (LL)V[A[i]];
for (int i = 0; i < 26; ++i) Mp[i].clear();
for (int i = 1; i <= l; ++i)
{
Ans += (LL)Mp[A[i]][Sum[i - 1]];
++Mp[A[i]][Sum[i]];
}
cout << Ans << endl;
return 0;
}
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】的更多相关文章
- [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】
题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x= ...
- CF#538(div2) B. Yet Another Array Partitioning Task 【YY】
任意门:http://codeforces.com/contest/1114/problem/B B. Yet Another Array Partitioning Task time limit p ...
- CF Round #580(div2)题解报告
CF Round #580(div2)题解报告 T1 T2 水题,不管 T3 构造题,证明大约感性理解一下 我们想既然存在解 \(|a[n + i] - a[i]| = 1\) 这是必须要满足的 既然 ...
- CF round #622 (div2)
CF Round 622 div2 A.简单模拟 B.数学 题意: 某人A参加一个比赛,共n人参加,有两轮,给定这两轮的名次x,y,总排名记为两轮排名和x+y,此值越小名次越前,并且对于与A同分者而言 ...
- [CF Round #295 div2] C. DNA Alignment 【观察与思考0.0】
题目链接:C. DNA Alignment 题目大意就不写了,因为叙述会比较麻烦..还是直接看英文题面吧. 题目分析 经过观察与思考,可以发现,构造的串 T 的每一个字符都与给定串 S 的每一个字符匹 ...
- A. Grasshopper And the String(CF ROUND 378 DIV2)
A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes input ...
- A. Alyona and Numbers(CF ROUND 358 DIV2)
A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
- CF Round#436 div2
额,这次的题目其实挺智障的.所以通过这次比赛,我也发现了自己是一个智障.... 不说太多,说多是泪... A. Fair Game 题意:给你一个数组,看你能否把它均分为两个所有元素均相同的子数组. ...
- CF Round #569 Div2(contest1180)
比赛链接:http://codeforces.com/contest/1180 Problem A 题意:给出n,问方块数.看图理解... Solution: 找一找规律就可以了,发现方块数为2n*( ...
随机推荐
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- LabVIEW系列——自定义错误
1.自定义错误代码文本文件在labview中三处: a).E:\Program Files\National Instruments\LabVIEW 8.6\project\errors ...
- C#绘制圆形时钟
本文由作者参考部分案例后加以修改完成: 参考链接如下: http://blog.csdn.net/xuemoyao/article/details/8001113 http://wenku.baidu ...
- Linux网络相关命令小结
# ifconfig # ifup/ifdown # route -n # ip link show //显示本机所有接口信息 # traceroute # netstat //查看本机网络连接与后门 ...
- 【锋利的jQuery】学习笔记02
第二章 jQuery选择器 一.jQuery选择器的优势 写法简洁 $("div") 支持css2和css3选择器(对于css3选择器支持这一项,我认为应该是jQuery首先创造并 ...
- jq版本更新后无live函数的处理.
之前你的代码如果是$("#ele").live("click", function() { //...});现在要写成$("#ele" ...
- [上传下载] C#FileDown文件下载类 (转载)
点击下载 FileDown.zip 主要功能如下 .参数为虚拟路径 .获取物理地址 .普通下载 .分块下载 .输出硬盘文件,提供下载 支持大文件.续传.速度限制.资源占用小 看下面代码吧 /// &l ...
- SQL求差集
数据库环境:SQL SERVER 2008R2 Sql Server有提供求集合差集的函数——EXCEPT.先看看EXCEPT的用法, { <query_specification> | ...
- Java SpringMvc+hibernate架构中,调用Oracle中的sp,传递数组参数
一.问题 我们调用数据,大都是可以直接获取表中的数据,或者用复杂点的sql语句组成的.但是,有时候,当这样达不到我们要的全部数据的时候,这时,我们就用到了存储过程[sp],如果sp需要参数是数组的话, ...
- phpmyadmin备份小问题
不要将imformation——shame或者mysql等备份,要有选择的备份表 关注我的新浪微博