zoukankan      html  css  js  c++  java
  • 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 megabytes
    input
    standard input
    output
    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?

    Input

    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.

    Output

    Print the answer to the problem.

    Sample test(s)
    Input
    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
    Output
    2
    Input
    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
    Output
    2
    Note

    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
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cstdio>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<map>
     9 #include<set>
    10 #include<stack>
    11 #include<string>
    12 
    13 #define N 100005
    14 #define M 10005
    15 #define mod 1000000007
    16 //#define p 10000007
    17 #define mod2 1000000000
    18 #define ll long long
    19 #define ull unsigned long long
    20 #define LL long long
    21 #define eps 1e-6
    22 //#define inf 2147483647
    23 #define maxi(a,b) (a)>(b)? (a) : (b)
    24 #define mini(a,b) (a)<(b)? (a) : (b)
    25 
    26 using namespace std;
    27 
    28 ll a[30];
    29 char s[N];
    30 int l;
    31 ll ans;
    32 map<ll,ll>mp[30];
    33 ll sum;
    34 
    35 void ini()
    36 {
    37     ans=0;
    38     int i;
    39     int te;
    40     for(i=1;i<26;i++){
    41         scanf("%I64d",&a[i]);
    42     }
    43     for(i=0;i<26;i++){
    44         mp[i].clear();
    45     }
    46     scanf("%s",s);
    47     l=strlen(s);
    48     sum=0;
    49     for(i=0;i<l;i++){
    50         te=s[i]-'a';
    51         ans+=mp[te][sum];
    52         sum+=a[te];
    53         mp[te][sum]++;
    54     }
    55 }
    56 
    57 void solve()
    58 {
    59 
    60 }
    61 
    62 void out()
    63 {
    64     printf("%I64d
    ",ans);
    65 }
    66 
    67 int main()
    68 {
    69     //freopen("data.in","r",stdin);
    70     //freopen("data.out","w",stdout);
    71     //scanf("%d",&T);
    72     //for(int ccnt=1;ccnt<=T;ccnt++)
    73     //while(T--)
    74     //scanf("%d%d",&n,&m);
    75     while(scanf("%I64d",&a[0])!=EOF)
    76     {
    77         ini();
    78         solve();
    79         out();
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    影响历史进程的三只苹果
    C语言实现的RSA算法程序(使用GMP)
    C++实现的大整数分解Pollard's rho算法程序
    Java实现的大整数分解Pollard's rho算法程序
    素性测试算法
    素性测试AKS算法程序
    C++实现的Miller-Rabin素性测试程序
    为什么天朝互联网的三巨头是BAT
    KMP模式匹配算法程序(Python,C++,C)
    I00014 汉若塔问题的C++程序
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4306972.html
Copyright © 2011-2022 走看看