codeforces 451 D.Count Good Substrings (思维)

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.

Sample test(s)
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.

题解:刚看一下题感觉无从下手的感觉,但是仔细想一下,当所有的a和b压缩后总会变成"......abababab......"的形式,所以要想是回文串只要是两头选的都是a或都是b即可,另外还有一个小技巧,在一个数列中奇数位和偶数位之间一定有偶数个字母,所以偶数的个数就是a或b的奇数个数 * 偶数的个数再相加就好了,而两个奇数位或两个偶数位之间一定有奇数个数,所以奇数的个数就是a或b的奇或偶的个数中任意取两个就好了,然后再相加,另外在注意一点这个题要用64位整数否则会溢出

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

详细的见代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
const int maxn=100000+10;

ll cal(ll x){
    return x*(x-1)/2;
}

int main(){
    char str[maxn];
    while(gets(str)){
        int n=strlen(str);
        ll odda=0,oddb=0,evena=0,evenb=0;
        for(int i=0;i<n;i++){
            if(i%2){//判断奇数位
                if(str[i]=='a') odda++;//奇数位的 a 个数加 1 
                else oddb++;//奇数位的 b 的个数加 1 
            }
            else{//判断偶数位
                if(str[i]=='a') evena++;//偶数位的 a 的个数加 1 
                else evenb++;//偶数位的 b 的个数加 1
            }
        }
        ll even=odda*evena+oddb*evenb;//总的偶数回文串的个数
        ll odd=cal(odda)+cal(evena)+cal(oddb)+cal(evenb)+n;//总的奇数的回文串的个数
        printf("%lld %lld\n",even,odd);
    }
    return 0;
}
欢迎大家有什么不懂的地方提出来

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值