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


转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值