题目链接: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】的更多相关文章

  1. [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= ...

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

  3. CF Round #580(div2)题解报告

    CF Round #580(div2)题解报告 T1 T2 水题,不管 T3 构造题,证明大约感性理解一下 我们想既然存在解 \(|a[n + i] - a[i]| = 1\) 这是必须要满足的 既然 ...

  4. CF round #622 (div2)

    CF Round 622 div2 A.简单模拟 B.数学 题意: 某人A参加一个比赛,共n人参加,有两轮,给定这两轮的名次x,y,总排名记为两轮排名和x+y,此值越小名次越前,并且对于与A同分者而言 ...

  5. [CF Round #295 div2] C. DNA Alignment 【观察与思考0.0】

    题目链接:C. DNA Alignment 题目大意就不写了,因为叙述会比较麻烦..还是直接看英文题面吧. 题目分析 经过观察与思考,可以发现,构造的串 T 的每一个字符都与给定串 S 的每一个字符匹 ...

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

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

  8. CF Round#436 div2

    额,这次的题目其实挺智障的.所以通过这次比赛,我也发现了自己是一个智障.... 不说太多,说多是泪... A. Fair Game 题意:给你一个数组,看你能否把它均分为两个所有元素均相同的子数组. ...

  9. CF Round #569 Div2(contest1180)

    比赛链接:http://codeforces.com/contest/1180 Problem A 题意:给出n,问方块数.看图理解... Solution: 找一找规律就可以了,发现方块数为2n*( ...

随机推荐

  1. android 62 手机存储目录的划分

    android下应用程序的路径和javase不同,应用程序的数据要保存自己的文件夹里面 > > getFileDir(); 获取自己的文件夹 /data/data/包名(应用程序的名字)/ ...

  2. DTrace Probes in HotSpot VM----java

    http://docs.oracle.com/javase/6/docs/technotes/guides/vm/dtrace.html http://docs.oracle.com/javase/7 ...

  3. WebService学习笔记系列(二)

    soap(简单对象访问协议),它是在http基础之上传递xml格式数据的协议.soap协议分为两个版本,soap1.1和soap1.2. 在学习webservice时我们有一个必备工具叫做tcpmon ...

  4. Ajax无刷新提交表单和显示

    ajax无刷新表单提交:   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  5. 初识 .NET平台下作业调度器——Quartz.NET

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  6. Oracle计算时间函数(numtodsinterval、numtoyminterval)

    numtodsinterval(<x>,<c>) ,x是一个数字,c是一个字符串,表明x的单位,这个函数把x转为interval day to second数据类型 常用的单位 ...

  7. PHP语言、浏览器、操作系统、IP、地理位置、ISP

    )]; } else { $Isp = 'None'; } return $Isp; }}

  8. ios专题 - GCD(1)

    什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像 NSOperationQueue,他们都允 ...

  9. C++ trivial和non-trivial构造函数及POD类型(转)

    原博客地址http://blog.csdn.net/a627088424/article/details/48595525 最近正纠结这个问题就转过来了,做了点补充(参考<深度探索C++对象模型 ...

  10. vector 与 set区别

    注:本文内容摘自网络,准确性有待验证,现阶段仅供学习参考.尊重作品作者成果,原文链接 :http://blog.csdn.net/wxdcxp/article/details/5279618 首先,v ...