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 ...
随机推荐
- C#签名验签帮助类
using System; using System.IO; using System.Text; using System.Collections.Generic; using System.Sec ...
- Java多线程(九):生产者消费者模型
生产者消费者模型 生产者:生产任务的个体: 消费者:消费任务的个体: 缓冲区:是生产者和消费者之间的媒介,对生产者和消费者解耦. 当 缓冲区元素为满,生产者无法生产,消费者继续消费: 缓冲区元素为空, ...
- Eclipse中项目本身没有问题,可是工程名却有红色小叉叉解决办法
右击项目“Properties”,在弹出的“Properties”的左侧边框,单击“Project Facets”,打开“Project Facets”页面, 在页面中“Java”下拉选项中,选择与自 ...
- StoneTab标签页CAD插件 3.1.0
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- VS 之github
VS 代码发布到TFS上 1. 登录 visualstudio.com. 进入 https://qgb.visualstudio.com Create Project 这里是相当于新建了一个文件夹 ...
- react用高阶组件实现路由守卫
react-router不像vue-router一样有很多钩子函数,可以做路由守卫.想实现路由守卫,可以用高阶组件来实现. @connect(state => ({ isLogin: state ...
- 【Day4】3.urllib模块使用案例
import urllib.request as ur ret = ur.urlopen('https://edu.csdn.net/').read() with open('edu.html','w ...
- MySQL跨表更新SQL
1 sql范式 把s表中的city_name的值设置为city表中的name,关联条件是city_code 和 code update student s, city c set s.city_na ...
- Hadoop_28_MapReduce_自定义 inputFormat
1. 自定义inputFormat 1.1.需求: 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件,此时就需要有相应解决方案; 1.2.分析: 小文件的优化 ...
- Java重写(Override)与重载(Overload)
方法的重写规则 参数列表必须完全与被重写方法的相同: 返回类型与被重写方法的返回类型可以不相同,但是必须是父类返回值的派生类(java5 及更早版本返回类型要一样,java7 及更高版本可以不同): ...