codeforces Round #258(div2) 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.

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。现在定义一个字串如若合并之后的字串是个回文字符串,就是一个good substrings,求出这样的字串有多少个,并输出长度为偶数和奇数的个数。

解法:

首先,我们需要注意到两个已知条件:

1. 字串可以合并,例如 abbaabbb 合并之后就是abab

2. 只有两个字符a,b

我们可以发现,合并之后的字串一定是aba或者abab类型的,那么合并之后的字串如果是回文的话,第一个字符肯定与最后一个字符相同,反之亦然。

我们可以进一步得出一个结论:两个不同的位置,字符相同,之间构成的字串一定是good substrings。总个数很容易在O(n)的时间复杂度求出来,但我们现在要求的是长度为奇数和偶数的个数,也就是拆开来,那我们就可以将求总个数的方法,拆一下。

位置为奇数的字符,与位置为偶数的字符构成偶数长度的good substrings,

与位置为奇数的字符构成奇数长度的good
substrings,

位置为偶数的字符,与位置为偶数的字符构成奇数长度的good
substrings,

与位置为奇数的字符构成偶数数长度的good substrings,

代码:

#include <string>
#include <iostream>
#define LL long long

using namespace std;

string st;
LL ansOdd, ansEven;
LL Odd[2], Even[2];

void init() {
	cin >> st;
}

void solve() {
	for (int i = 0; i < st.size(); i++) {
		ansOdd++;
		int j = st[i]-'a';

		if ((i+1)&1) {
			ansOdd += Odd[j];
			ansEven += Even[j];
			Odd[j]++;
		}
		else {
			ansEven += Odd[j];
			ansOdd += Even[j];
			Even[j]++;
		}
	}

	cout << ansEven << ' ' << ansOdd << endl;
}

int main() {
	init();
	solve();
}
时间: 2024-10-10 20:56:57

codeforces Round #258(div2) D解题报告的相关文章

codeforces Round #258(div2) C解题报告

C. Predict Outcome of the Game time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n games in a football tournament. Three teams are participating in it. Currently k games had alread

codeforces Round #260(div2) D解题报告

D. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of n non-em

codeforces Round #261(div2) D解题报告

D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partn

codeforces Round #259(div2) B解题报告

B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a sequence of integers a1,?a2,?...,?an in non-decreas

codeforces Round #259(div2) C解题报告

C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept

codeforces Round #259(div2) D解题报告

D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony

codeforces Round #259(div2) E解题报告

E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Ce

codeforces Round #261(div2) A解题报告

C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school

codeforces Round #275(div2) D解题报告

D. Interesting Array time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output We'll call an array of n non-negative integers a[1],?a[2],?...,?a[n] interesting, if it meets m constraints. The i-th of