Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
题意:
对于26个字母 每个字母分别有一个权值
给出一个字符串,找出有多少个满足条件的子串,
条件:1、第一个字母和最后一个相同,2、除了第一个和最后一个字母外,其他的权和为0
思路:
预处理出sum[i]:s[0~i]的和
开26个map<LL, LL>numV 分别表示 每个字母前缀和 的个数
例如处理到第i个元素,且numV[s[i]-'a'][sum[i]-v[s[i] - 'a']] (值为2)
表示在处理到的元素之前 以s[i]结尾的前缀和为sum[i]-v[s[i]-'a']的个数有2个
所以答案就要加2(因为前面已经组成过这个值,又出现的原因就是他的值变为了0)
const int maxv = ;
int v[maxv];
map<LL, LL> numV[maxv]; const int maxn = + ;
char s[maxn];
LL sum[maxn];
void init()
{
for (int i = ; i < ; i++)
{
scanf("%d", v + i);
}
scanf(" ");
gets(s);
} void solve()
{
int len = strlen(s);
sum[] = v[s[]-'a'];
for (int i = ; i < len; i++)
{
sum[i] = sum[i-] + v[s[i]-'a'];
} LL ans = ;
for (int i = ; i < len; i++)
{
ans += numV[s[i]-'a'][sum[i] - v[s[i] - 'a']];
numV[s[i]-'a'][sum[i]]++;
}
printf("%lld\n", ans);
} int main()
{
init();
solve();
return ;
}
Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings的更多相关文章
- 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 ...
- Codeforces Round #294 (Div. 2)
水 A. A and B and Chess /* 水题 */ #include <cstdio> #include <algorithm> #include <iost ...
- Codeforces Round #294 (Div. 2)C - A and B and Team Training 水题
C. A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #294 (Div. 2)B - A and B and Compilation Errors 水题
B. A and B and Compilation Errors time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces Round #294 (Div. 2)A - A and B and Chess 水题
A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)
A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- PHP store session with couchbase
如何用couchbase存储session 有两种常见方式:1.采用memcache模式连接couchbase 只需两句修改: ini_set('session.save_handler', 'mem ...
- if 判断中出现逗号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Large Margin DAGs for Multiclass Classification
Abstract We present a new learning architecture: the Decision Directed Acyclic Graph (DDAG), which i ...
- HeadFirst 设计模式
一.设计原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 为交互对象之间的松紧耦合设计而努力 对扩展开放,都修稿关闭 依赖抽象,不要依赖具体类 最少知识原则:之和朋友交谈 好莱坞原则: ...
- unix下输出重定向
> 为重定向符号 >> 重定向不覆盖原文件内容 example: 1. 标准输出重定向 echo "123" > /home/123.txt ---- 标准 ...
- hdu 2088
ps: 超级超级水的一道题....就是格式问题要注意... 除了第一个数据,其他的每个数据都要有个空行比如: 4 1 2 3 4 3 4 //这是空行 1 2 3 4 3 4 //这是空行 代码: ...
- C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期
C# 时间计算 今天.昨天.前天.明天 class Program { static void Main(string[] args) { ...
- docker基本操作
centos 7 安装docker 目前,CentOS 仅发行版本中的内核支持 Docker. Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上. Dock ...
- 【Python】str类方法说明
#capitalize():字符串首字符大写 string = 'this is a string.'new_str = string.capitalize()print(new_str)#输出:Th ...
- CURL常用命令
下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://www.centos.org 通过-o/-O选项保存下载的文件到指定的文件中:-o:将文件保存为命令行中指定的文件名 ...