Codeforces Round #258 (Div. 2)-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.

Examples
input
bb
output
1 2
input
baab
output
2 4
input
babb
output
2 5
input
babaa
output
2 7
在两个相同则字符之间的串一定是good string

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define maxn 1000005
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long ll;

char str[maxn];
ll get(int m){
	return (ll)m * (m-1) / 2;
}
int main(){
//	freopen("in.txt", "r", stdin);
	scanf("%s", str);
	int p1 = 0, p2 = 0;
	int k1 = 0 , k2 = 0;
	for(int i = 0; str[i]; i++){
		if(str[i] == 'a'){
			if(i&1)
			 p1++;
			else
			 p2++;
		}
		else{
			if(i&1)
			 k1++;
			else
			 k2++;
		}
	}
	ll odd, even;
	even = (ll)p1 * p2 + (ll)k1 * k2;
	odd = get(p1) + get(p2) + get(k1) + get(k2) + strlen(str);
	cout << even << " " << odd << endl;
	
	return 0; 
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值