Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
2 seconds
256 megabytes
standard input
standard output
A and B are preparing themselves for programming contests.
After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.
A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).
B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).
Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.
Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?
The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively.
The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.
Print the answer to the problem.
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1
xabcab
2
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1
aaa
2
In the first sample test strings satisfying the condition above are abca and bcab.
In the second sample test strings satisfying the condition above are two occurences of aa.
没想到用前缀和,弱爆了
10091856 | 2015-03-01 08:23:44 | njczy2010 | D - A and B and Interesting Substrings | GNU C++ | Accepted | 62 ms | 7976 KB |
10090622 | 2015-03-01 04:45:07 | njczy2010 | D - A and B and Interesting Substrings | GNU C++ | Time limit exceeded on test 6 | 2000 ms | 12200 KB |
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100005
#define M 10005
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define ull unsigned long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll a[];
char s[N];
int l;
ll ans;
map<ll,ll>mp[];
ll sum; void ini()
{
ans=;
int i;
int te;
for(i=;i<;i++){
scanf("%I64d",&a[i]);
}
for(i=;i<;i++){
mp[i].clear();
}
scanf("%s",s);
l=strlen(s);
sum=;
for(i=;i<l;i++){
te=s[i]-'a';
ans+=mp[te][sum];
sum+=a[te];
mp[te][sum]++;
}
} void solve()
{ } void out()
{
printf("%I64d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%I64d",&a[])!=EOF)
{
ini();
solve();
out();
}
return ;
}
Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]的更多相关文章
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
题意: 对于26个字母 每个字母分别有一个权值 给出一个字符串,找出有多少个满足条件的子串, 条件:1.第一个字母和最后一个相同,2.除了第一个和最后一个字母外,其他的权和为0 思路: 预处理出sum ...
- 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)
水 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 #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp
C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
随机推荐
- 序列化pickle模块
1.pickle模块 pickle.dumps() 和pickle.loads() import pickle f = open('112.pkl','w') a = {'name':2,2:3,3: ...
- asp.net 中文部分显示问号
很神奇的事情,今天部署了一个网站,页面从数据读取新闻后,有些新闻标题全部显示问题号,有几个新闻能正确显示汉字,然后查看新闻页面又能正常显示汉字. 解决办法: 在异常的页面上加上 < %@ COD ...
- kde framework概述(KDE Framework译文)
KDE Frameworks 基于QT框架,提供简单实用的类(例如那些KCoreAddons里的类)去为桌面应用的日常需要整合出解决方案(例如KNewStuff用于在应用中获取可下载的附加内容,或者那 ...
- jacob的使用方法
网上一大堆你抄我的,我抄你的,但基本配置都没说清,做个笔记让后来的人少走冤枉路 1.下载最新的jacob,jdk版本一一对应,1.6对应jacob的1.16,1.7对应1.17.... 2.应用程序将 ...
- MFC技术积累——基于MFC对话框类的那些事儿2
3. 绘图 3.1 对话框资源编辑 首先通过添加控件的方式来创建一个简单的绘图对话框如图所示,创建步骤为: 第一.在VC++6.0软件环境的灰色空白区域右击,选中Controls,然后会弹出一个控件对 ...
- (转)Spring的bean管理(注解方式)
http://blog.csdn.net/yerenyuan_pku/article/details/69663779 Spring的bean管理(注解方式) 注解:代码中的特殊标记,注解可以使用在类 ...
- Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL
一.IPC inter process communication 进程间通讯 二.AIDL android interface defination language 安卓接口定义语言 满 ...
- zabbix设置多个收件人
1.建群组 2.添加群组权限 3.添加用户,归属到上面新建的组 4.动作里发送消息给新建的组 5.这样设置后,管理员账号不用设置收件媒介
- iOS之绘制像素到屏幕
译注:这篇文章虽然比较长,但是里面的内容还是很有价值的. 像素是如何绘制到屏幕上面的?把数据输出到屏幕的方法有很多,通过调用很多不同的framework和不同的函数.这里我们讲一下这个过程背后的东西. ...
- 2012 noip提高 Vigenère 密码
P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de VigenèreBlaisedeVigene`re 设计了一种多表密码加密算法―― VigenèreVigene ...