


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:
- the number of good substrings of even length;
- 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 longusing namespace std;string st;LL ansOdd, ansEven;LL Odd[2], Even[2];void init() { cin >> st;}void solve() { for (int i = 0; i <p></p> </iostream></string>

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),