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;
    }
    


  • 相关阅读:
    ASP.NET Web API是如何根据请求选择Action的?[上篇]
    Ruby的对象模型
    MongoDB学习3
    Linux目录树详细说明
    Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)
    [置顶] SQL注入安全分析
    3.9 聚集和联接
    Qt之QTemporaryFile(文件名唯一,且可以自动删除)
    调用Windows属性窗口(居然是通过注册表来调用的)
    QTextEdit中选中文本修改字体与颜色,全部文本修改字体与颜色(设置调色板的前景色、背景色、文字颜色以及基色)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538658.html
Copyright © 2011-2022 走看看