zoukankan      html  css  js  c++  java
  • A. Even Substrings

    A. Even Substrings

    time limit per test
    0.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a string s=s1s2sns=s1s2…sn of length nn, which only contains digits 11, 22, ..., 99.

    A substring s[lr]s[l…r] of ss is a string slsl+1sl+2srslsl+1sl+2…sr. A substring s[lr]s[l…r] of ss is called even if the number represented by it is even.

    Find the number of even substrings of ss. Note, that even if some substrings are equal as strings, but have different ll and rr, they are counted as different substrings.

    Input

    The first line contains an integer nn (1n650001≤n≤65000) — the length of the string ss.

    The second line contains a string ss of length nn. The string ss consists only of digits 11, 22, ..., 99.

    Output

    Print the number of even substrings of ss.

    Examples

    input

    4
    1234
    

    output

    6

    input

    4
    2244
    

    output

    10

    Note

    In the first example, the [l,r][l,r] pairs corresponding to even substrings are:

    • s[12]s[1…2]
    • s[22]s[2…2]
    • s[14]s[1…4]
    • s[24]s[2…4]
    • s[34]s[3…4]
    • s[44]s[4…4]

    In the second example, all 1010 substrings of ss are even substrings. Note, that while substrings s[11]s[1…1] and s[22]s[2…2] both define the substring "2", they are still counted as different substrings.

    题意:任取主串中的一个区间,判断这个是减是不是偶数,然后是偶数的话,就数量加1,找出有多少个这样的偶数区间。比如:第一个样例1234,其中12,2,1234,234,34,4为偶数,所以答案是6。其思路是从第一个字符遍历到最后一个字符,如果遇到偶数,就将这个数的位数加入sum中,依次遍历过就行。

    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    int main()
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int sum=0;
        for(int i=0;i<n;i++)
            if((s[i]-'0')%2==0)    //判断当前数字是不是偶数
                sum+=i+1;            //加上当前数字的位数
        cout<<sum<<endl;
        return 0;
    }
  • 相关阅读:
    (转)python字符串函数
    分享二:架构设计分享一:关于API分布式服务提供方式
    架构设计分享一:关于分布式系统的数据一致性问题(一)
    分享四:分布式事务设计-两段式提交
    分享三:mysql跨库查询
    PHP笔试题
    我的mysql学习心得
    linux分享一:进程全攻略--守护进程(服务)
    分享二:签名原理与算法
    linux命令详解:pgrep命令
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/10632609.html
Copyright © 2011-2022 走看看