CodeForces 518D 问一个字符串里有多少子串首尾字符相同,去掉首尾中间价值和为0 :map/技巧

记录前缀价值和,开26个map保存某字符的某价值出现的次数,再次出现加上即可=

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<map>
 4 #include<algorithm>
 5 using namespace std;
 6 map<long long,long long>mp[28];
 7 char s[100005];
 8 int main()
 9 {
10   long long i,len,ans,tmp,a[30];
11   for (i=0;i<26;i++) scanf("%I64d",&a[i]);
12   scanf("%s",s);
13   len=strlen(s);
14   tmp=ans=0;
15   for (i=0;i<len;i++)
16   {
17     ans+=mp[s[i]-'a'][tmp];
18     tmp+=a[s[i]-'a'];
19     mp[s[i]-'a'][tmp]++;
20   }
21   printf("%I64d\n",ans);
22 }
View Code

题目链接:http://codeforces.com/contest/519/problem/D

posted on 2015-03-01 22:53  xiao_xin  阅读(419)  评论(0编辑  收藏  举报

导航