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 ...
随机推荐
- WindowsAPI操作串口
#include <windows.h> #include <stdio.h> int main() { //1.打开串口 HANDLE hCom; hCom = Create ...
- 多线程之thread和runnable
Runnanle方式可以避免Thread由于单继承特性带来的缺陷. Runnable代码可以被多个线程(thread实例)共享,适用于多个线程处理同一资源的情况. 线程的生命周期:创建,就绪,阻塞,运 ...
- redis 交集、并集、差集
sinter .sunion .sdiff redis 支持 Set集合的数据存储,其中有三个比较特殊的方法: sinter key [key …] 返回一个集合的全部成员,该集合是所有给定集合的交集 ...
- elementui-插槽
<el-table-column label="操作"> <template slot-scope="scope"> <el-bu ...
- docker 第六篇 dockerfile
复习下镜像生成途径 Dockerfile 基于容器制作 什么是dockerfile: 用来构建镜像的源码,在配置文件中调用命令,这些命令是用来生成docker镜像的. dockerfile的语法格式: ...
- c# datatable 如何转CSV文件
public void DataTableToCSV(DataTable dtCSV, string csvFileFullName, bool writeHeader, string delimet ...
- MVP架构的一个小例子
主角: MVP是一种编程的架构模式,M=Model,负责提供数据:V=View,负责显示数据:P=Presenter,负责处理数据. 应用例子: csharp写的一个qq机器人. 一.Model层 获 ...
- dedecms:限制栏目列表生成的最大页数防止被采集
dedecms:限制栏目列表生成的最大页数防止被采集 如果您的网站数据量较大,列表很多的话甚至达到上千页,生成列表时就特别耗费时间,这个缺点可以被优化掉:网站好不容易建起来,担心网站内容被采集走,如果 ...
- Windows7用VirtualBox虚拟Ubuntu共享文件夹的终极方式
在Win7用VirtualBox虚拟机安装Ubuntu后,共享文件夹再也不用手工mount了 安装增强工具包 设置共享文件夹后 VB已经自动挂载Windows文件夹到 /media/sf_*** 目录 ...
- mysql 忘记密码如何修改
第一步:将服务停掉 /etc/init.d/mysqld stop 第二步:加参数启动服务 cd /application/mysql/bin/ mysqld_safe --skip-grant-ta ...