[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*( ...
随机推荐
- iBatis自动生成的主键 (Oracle,MS Sql Server,MySQL)【转】
iBatis的sqlMap配置文件的selectKey元素有个type属性,可以指定pre或者post表示前生成(pre)还是后生成(post). Oracle设置 <!-- Oracle SE ...
- MySQLdb安装和使用2
http://blog.chinaunix.net/uid-8487640-id-3183185.html MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: ...
- RedHat7配置Nginx实现多域名虚拟主机的SSL/TLS认证(实现单IP以不同证书服务于不同域名)
以RedHat7(64bit)平台为例 如果RedHat源没法用,可以使用EPEL源 # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-rel ...
- 一个小时成功安装SQL Server 2012图解教程
在安装微软最新数据库SQL Server 2012之前,编者先确定一下安装环境:Windonws 7 SP1,32位操作系统.CPU是2.1GHz赛扬双核T3500,内存2.93GB. 安装SQL S ...
- Linux重复执行上一条命令
执行刚刚执行的一条命令: !! 执行最近一个以指定字符串开头的命令(比如man) !man !m 引用上一个命令的最后一个参数 !$ <ESC>, .
- 第六篇:web之python框架之django
python框架之django python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django te ...
- ASP.NET MVC 第六回 过滤器Filter
在Asp.netMvc中当你有以下及类似以下需求时你可以使用Filter功能 判断登录与否或用户权限 决策输出缓存 防盗链 防蜘蛛 本地化与国际化设置 实现动态Action Filter是一种声明式编 ...
- [转]eclipse github 提交代码
1 git add2 git commit3 git pull (会产生冲突) 分成自动合并和手动合并4 处理冲突的文件 5 git push 本次commit 我用的是Eclipse的插件EGit ...
- HTML语言语法大全
(文章转载至博客园 dodo-yufan) <! - - ... - -> 註解 <!> 跑馬燈 <marquee>...</marquee>普通捲動 ...
- Android 输入法键盘和activity页面遮挡问题解决
本文主要介绍Android中如何解决输入法键盘和activity页面遮挡的问题. 总结: 不希望遮挡设置activity属性android:windowSoftInputMode="adju ...