zoukankan      html  css  js  c++  java
  • Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学

    题目链接:http://codeforces.com/problemset/problem/451/D


    D. Count Good Substrings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".

    Given a string, you have to find two values:

    1. the number of good substrings of even length;
    2. the number of good substrings of odd length.
    Input

    The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.

    Output

    Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.

    Examples
    input
    bb
    
    output
    1 2
    
    input
    baab
    
    output
    2 4
    
    input
    babb
    
    output
    2 5
    
    input
    babaa
    
    output
    2 7
    
    Note

    In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length.

    In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length.

    In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length.

    Definitions

    A substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.

    A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1.




    题解:

    1.分别记录‘a’在奇数位置、偶数位置出现的次数, ‘b’亦如此。

    2.长度为偶数的情况:相同的字母一个出现在奇数位置, 一个出现在偶数位置,假设出现次数分别为n、m, 则n*m。

    3.长度为偶数的情况:相同的字母都出现在偶数位置或者奇数位置。




    代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const double eps = 1e-6;
    const int INF = 2e9;
    const LL LNF = 9e18;
    const int mod = 1e9+7;
    const int maxn = 1e5+10;
    
    char s[maxn];
    int a[2][2];
    
    LL f(int x) { return 1LL*x*(x-1)/2; }
    
    int main()
    {
        scanf("%s",s+1);
        int len = strlen(s+1);
        for(int i = 1; i<=len; i++)
            a[s[i]!='a'][i&1]++;
    
        LL even = 1LL*a[0][0]*a[0][1] + 1LL*a[1][0]*a[1][1];
        LL odd = f(a[0][0]) + f(a[0][1]) + f(a[1][0]) + f(a[1][1]) + len;
        cout<<even<<' '<<odd<<endl;
    }
    


  • 相关阅读:
    tpot从elastic search拉攻击数据之三 用于拉取的java程序
    tpot从elastic search拉攻击数据之二 配置端口映射
    wireshark使用
    VS c++ opencv画图
    java maven项目打包
    从es中拉取全部数据/大量数据 使用scroll+scan避免深分页
    java配置文件properties,yml,一般文件
    解决:JQuery "Uncaught ReferenceError: $ is not defined"错误
    java 字符串解析为json 使用org.json包的JSONObject+JSONArray
    easyui最简单的左右布局实现,及tab的右键菜单实现
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538658.html
Copyright © 2011-2022 走看看