CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)
题意:给出26个字母每个字母的价值,问字符串中有多少个满足以下条件的子串:
1.子串的第一个和最后一个相同
2.子串除了头和尾的其他字符的价值加起来和尾0
这题普通方法应该是O(n^2),但是在1e5的条件下肯定会超时,所以学习了大力学长奥义的O(n)方法。具体方法也说不清楚,看代码吧,很短,也容易看懂。只能说,相当奥义的方法。。
代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
typedef long long ll; char s[+];
ll val[+];
map<ll,int> M[]; int main()
{
for(int i=;i<;i++) scanf("%I64d",val+i);
scanf("%s",s+);
int len = strlen(s+); ll pre = ,ans = ;
for(int i=;i<=len;i++)
{
int m = s[i] - 'a';
ans += M[m][pre];
pre += val[m];
M[m][pre] ++;
}
printf("%I64d\n",ans);
}
CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)的更多相关文章
- Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)
题目链接:http://codeforces.com/problemset/problem/519/D 题目大意:给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值.求子串t满足t的 ...
- Codeforces Round #306 (Div. 2) A. Two Substrings【字符串/判断所给的字符串中是否包含不重叠的“BA” “AB”两个字符串】
A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 ...
- 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 ...
- CF519 ABCD D. A and B and Interesting Substrings(map,好题)
A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...
- [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...
- 水题 Codeforces Round #306 (Div. 2) A. Two Substrings
题目传送门 /* 水题:遍历一边先找AB,再BA,再遍历一边先找BA,再AB,两种情况满足一种就YES */ #include <cstdio> #include <iostream ...
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
题意: 对于26个字母 每个字母分别有一个权值 给出一个字符串,找出有多少个满足条件的子串, 条件:1.第一个字母和最后一个相同,2.除了第一个和最后一个字母外,其他的权和为0 思路: 预处理出sum ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
随机推荐
- MangoDB CSharp Driver
1.引用MongoDB for C# Driver 从网上下载C#访问MongoDB的驱动,得到两个DLL: MongoDB.Driver.dll MongoDB.Bson.dll 将它们引用到项目中 ...
- C# enum枚举知识总结
C#中除了简单的变量类型外,还提供了三种较为复杂的变量类型,包括枚举.结构和数组.本文主要讲述枚举相关的知识. 枚举类型(也称为枚举),提供了一种有效的方式,来定义可能分配给变量的一组已命名的整数常量 ...
- 手工实现HttpBasic校验
HttpBasic: 是RFC中定义的一种控制HTTP协议访问资源的方式.具体当HTTP请求受限资源时,就需要在请求头中添加以"Authorization"为key的heade ...
- bash shell脚本之成员变量
shell中变量的使用 cat test3: #!/bin/bash # testing variables days= guest="Katie" echo "$gue ...
- django 文件上传样例以及遇到的一些问题
使用django上传文件 主流有两种方法 from表单以及ajax,为了自由度高一点,选择了ajax来实现文件的上传 前端部分代码如下: 主要关注 一 有一个文件上传(type='file')的按钮, ...
- 【jekins】安装jdk遇到的坑
首先我电脑版本为win10 64 在安装JDK时先安装了jdk包,安装路径为D:\Java\jdk1.8.0_171,装完jdk后,自动安装jre,我将其安装在D:\Java\jre1.8.0_171 ...
- TCP的keepalive和应用层的heart
从长链接说起 TCP是长链接的,也就是说连接建立后,及时数年没有通信连接仍然存在.这样做的好处是:免去了DNS解析的时间,连接建立等时间,大大加快了请求的速度,同时也有利于接受服务器的实时消息.但前提 ...
- MySQL数据库常见问题1:关于 “ MySQL Installer is running in Community mode ” 的解决办法
现象: MYSQL在安装完成后,系统能正常运行,但是第二天出现了如下一个提示框,如下图: 给个人人都看得懂的如下图: 解决办法: 这个是新版本MySQL服务自带的一个定时任务,每天23: ...
- Mysql系列-性能优化神器EXPLAIN使用介绍及分析
MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化. EXPLAIN 命令用法十分简单, 在 SEL ...
- liunx加载JX2410标准配置文件