Codeforces round #258 (div2) d Problem Solving report

Source: Internet
Author: User

D. Count good substringstime limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 LengthN(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 substringS[L,?R] (1? ≤?L? ≤?R? ≤?N) Of stringS? =?S1S2...SNIs stringSLSL? +? 1...SR.

A stringS? =?S1S2...SNIs a palindrome if it is equal to stringSNSN? -? 1...S1.

Question:

A string containing only a and B. Now we define a string. If the merged string is a return string, it is a good substrings. Then we can find the number of such strings and output the numbers of even and odd numbers.

Solution:

First, we need to note two known conditions:

1. Strings can be merged. For example, after merging abbaabbb, It is Abab.

2. Only two characters, A and B

We can find that the merged strings must be of the ABA or Abab type. If the merged strings are in the background, the first character must be the same as the last character, and vice versa.

We can further draw a conclusion: the strings between two different positions with the same characters must be good substrings. The total number is easily obtained at the time complexity of O (N), but we now need to split the numbers with odd and even numbers in length, then we can split the method for finding the total number.

A good substrings with an even number of characters with an odd position,

A good substrings with an odd number of characters,

A character with an even position, and a character with an even position constitute a good substrings with an odd length,

A good substrings with an even number of characters with an odd position,

Code:

#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 < 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();}

Codeforces round #258 (div2) d Problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.