[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*( ...
随机推荐
- [TypeScript ] What Happens to Compiled Interfaces
This lesson covers using your first TypeScript Interface and what happens to the Interface when it i ...
- [D3] 12. Basic Transitions with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- android开发之记录ListView滚动位置
这个问题本身不难,但是由于项目中的需求太过于复杂,结果导致这个问题变得不是那么容易实现.在网上一搜,结果如下: 我不知道是who copy who?反正介绍的所谓的三种方法,第一种都是无法运行的,很明 ...
- ubuntu文件管理常用命令 分类: linux ubuntu 学习笔记 2015-07-02 16:57 29人阅读 评论(0) 收藏
1.关闭防火墙:ufw disable 2.以.开头的表示隐藏文件 3..和..分别代表当前目录以及当前目录的父目录 4.显示当前用户所在目录pwd 5.touch创建空文件 6.mkdir创建新目录 ...
- Android开发之设定Dialog的位置
今天自定义了一个对话框,但是弹出时默认是显示在屏幕中间.主要代码:menuDialog = new AlertDialog.Builder(this).create(); ...
- Android canvas rotate():平移旋转坐标系至任意原点任意角度-------附:android反三角函数小结
自然状态下,坐标系以屏幕左上角为原点,向右是x正轴,向下是y正轴.现在要使坐标系的原点平移至任一点O(x,y),且旋转a角度,如何实现? 交待下我的问题背景,已知屏幕上有两点p1和p2,构成直线l.我 ...
- char nvarchar varchar
1. char 固定长度,最长n个字符. 2. varchar 最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同) char和varchar区别: ...
- 覆盖(override)和重载(overload)
覆盖(override)重写和 重载(overload) 继承,重写--->多态 我懂了,你懂吗 ,不看看文章 java 子类重写父类的方法应注意的问题 Java多态性理解
- IPX/SPX
转自百度百科 方便阅读 IPX/SPX 目 录 1英文原义 2中文释义 3IPX协议 3.1 说明 3.2 应用 4SPX协议 4.1 说明 4.2 应用 1英文原义 IPX/SPX 2中 ...
- 初学HTML5系列三:事件
Window 事件属性 针对 window 对象触发的事件(应用到 <body> 标签): 属性 值 描述 onafterprint script 文档打印之后运行的脚本. onbefor ...