Count Good Substrings

Count Good Substrings

Time Limit: 2000ms

Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 451D
64-bit integer IO format: %I64d      Java class name: (Any)

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 Input

Input

bb

Output

1 2

Input

baab

Output

2 4

Input

babb

Output

2 5

Input

babaa

Output

2 7

Hint

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.

Source

Codeforces Round #258 (Div. 2)

解题:压缩连续相同的字符成一个!最后剩下的就是a和b相间的串了。我们需要的是压缩后的回文串。当然只能首位相同的子串了。

如何在线性时间内找出原串里首位相同的串的个数呢?

由于只有两种字符‘a‘和’b‘,还有需要注意一个细节!自然数中,奇数到奇数的闭区间包含奇数个数。奇数到偶数的闭区间包含偶数个数。偶数到偶数的闭区间一定包含偶数个数。

长度为奇数的串要么是偶数到偶数,要么是奇数到奇数。同理可以得出长度为偶数的串只能由奇数到偶数 或者 偶数到奇数!

比如说某个奇数位置上是a,假设以它作为尾部压缩后回文的串有多少个呢?看首部,首部是奇数位置,那肯定是奇数串,首部是偶数,肯定是偶数串!那到底有多少个奇数串呢?

你能在前面找出多少个含a的且是奇数的位置,就能拼出多少个以‘a‘结尾的压缩后回文的串!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 LL odd[2],even[2],x,y;
17 char str[100100];
18 int main(){
19     while(~scanf("%s",str)){
20         memset(odd,0,sizeof(odd));
21         memset(even,0,sizeof(even));
22         x = y = 0;
23         for(int i = 0; str[i]; i++){
24             int sb = str[i]-‘a‘;
25             if(i&1){
26                 x += odd[sb];//奇数到奇数是奇数
27                 y += even[sb];//奇数到偶数是偶数
28                 odd[sb]++;
29             }else{
30                 x += even[sb];
31                 y += odd[sb];
32                 even[sb]++;
33             }
34         }
35         printf("%I64d %I64d\n",y,x+strlen(str));
36     }
37     return 0;
38 }

Count Good Substrings

时间: 2024-11-05 19:35:40

Count Good Substrings的相关文章

CodeForces 451D Count Good Substrings

哎,最近都在做图论,没有练DP,现在一遇到DP就不会了= = 因为有合并这个操作,所以只要是首位相同的字符串肯定是能够构成good串的,那么只要统计在奇数位上出现的0,1的个数和偶数位数,随便递推一下就出来了 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #i

codefroces451D - Count Good Substrings 数位DP

题意:给你n,m 问你n-m中有多少个数首位等于末位. 解题思路:数位DP,从0-n有多少个,这样分开计算,首先把每一位所有可能都枚举出来,然后在一位一位的比对DP 解题代码: 1 // File Name: 204a.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月25日 星期五 09时16分57秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8

Codeforces Round #258 D Count Good Substrings --计数

题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式,所以是good substrings,那么首尾字符肯定相同,于是就好搞了. 用:odd[0],odd[1]分别记录奇数位置上出现的a和b的个数,even[0],even[1]分别记录偶数位置上的a,b个数. 那么到一个奇数点时,奇数长度的子串个数应该加上奇数位置的该字符的个数,偶数长度的应该加上偶

Codeforces 451D Count Good Substrings(组合数学)

题目链接:Codeforces 451D Count Good Substrings 题目大意:定义good string,就是就一个字符串的连续相同字符用一个该字符替代后,形成回文串的字符串.现在给出一个字符串,问说该字符串的子串中,为good string的串有多少个,分长度为奇数和偶数的输出. 解题思路:因为字符串的组成为a和b,所以只要是头尾相同的子串都是满足的.所以我们计算在奇数和偶数位置的奇数个数和偶数个数即可,然后用组合数学求出答案. #include <cstdio> #inc

696. Count Binary Substrings - LeetCode

Question 696. Count Binary Substrings Example 1: Input: "00110011" Output: 6 Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011&qu

LeetCode 696. Count Binary Substrings

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. Substrings that occur multiple times are counted the numbe

[leetcode] Count Binary Substrings

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. Substrings that occur multiple times are counted the numbe

[LeetCode&amp;Python] Problem 696. Count Binary Substrings

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. Substrings that occur multiple times are counted the numbe

LeetCode算法题-Count Binary Substrings(Java实现)

这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串的数量,并且这些子串中的所有0和所有1都是连续的.重复出现的子串也计算在内.例如: 输入:"00110011" 输出:6 说明:有6个子串具有相同数量的连续1和0:"0011","01","1100","10"