`
Simone_chou
  • 浏览: 192719 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Count Good Substrings(数学)

    博客分类:
  • CF
 
阅读更多
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 组成,连续几个相同则可以合并看成一个,即 abbba 看为 aba,寻找偶数长度的回文串个数和奇数长度的回文串个数。

 

       思路:

       数学。首先要清楚,因为可以合并看成一个,所以首尾相同的两个字母则一定是回文串。偶数长度的回文串首尾两个下标必定是一奇一偶的相同字符组成,奇数长度的回文串必定是下标相同奇偶性的相同字符组成。所以问题就转化为了,求 a 的奇数下标的个数 和 偶数下标的个数,b 也是如此。记得结果要用 long long。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

char str[100005];

ll C(ll a) {
    return a * (a - 1) / 2;
}

int main() {

    ll oa = 0, ea = 0, ob = 0, eb = 0;
    scanf("%s", str);

    for (int i = 0; i < strlen(str); ++i) {
        if (str[i] == 'a') {
            if ((i + 1) % 2) ++oa;
            else ++ea;
        } else {
            if ((i + 1) % 2) ++ob;
            else ++eb;
        }
    }

    ll odd = oa * ea + ob * eb;
    ll even = strlen(str) + C(oa) + C(ob) + C(ea) + C(eb);

    printf("%I64d %I64d\n", odd, even);

    return 0;
}

 

 

分享到:
评论

相关推荐

    python-leetcode题解之Binary String With Substrings Representing

    python python_leetcode题解之Binary String With Substrings Representing

    Palindromic Substrings(C#).md

    题目名称:**Palindromic Substrings** 题目描述: 给定一个字符串 `s`,你需要找到并输出该字符串中所有长度为偶数的回文子串的数量。 输入: 输入包含一行,为一个字符串 `s`(只包含小写字母,长度不超过 `10^5...

    java-leetcode题解之Get Equal Substrings Within Budget.java

    java java_leetcode题解之Get Equal Substrings Within Budget.java

    c代码-计算 ab ba的个数

    void count_substrings(char *str, int *count_ab, int *count_ba) { // 子串计数的逻辑 } int main() { char input[100]; // 假设最大输入长度为100 int count_ab = 0, count_ba = 0; printf("请输入字符串: ...

    Java实现 蓝桥杯 算法提高VIP Substrings(暴力)

    试题 算法提高 Substrings 问题描述  You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring...

    delphi之分割字符串

    SubStrings[Matches.Count] := Copy(Source, Matches[Matches.Count - 1].Index + Matches[Matches.Count - 1].Length, MaxInt); end; ``` 总结,Delphi提供了多种方式来处理字符串分割,从简单的`SplitString`...

    c代码-计算ab ba的个数 方法一

    在`main.c`文件中,这个逻辑可能被封装在一个名为`count_substrings`的函数中,该函数接受一个字符串参数,并返回这两个计数值。而`README.txt`文件可能包含了关于如何编译和运行程序的说明,以及可能的输入示例和...

    华为上机考试题02.docx

    根据给定的信息,我们可以总结出以下几个关键的编程问题及其解决方案: ...这些题目涵盖了数组操作、字符串处理、数学运算等多个方面,对于准备参加华为技术面试的应聘者来说具有一定的参考价值。

    DIRegEx v7.0.0 XE2/XE3 32/64 Cracked

    Matching and extraction of matches / substrings from the source text. Searching for regular expressions within streams and memory buffers. To search within streams or files (of virtually unlimited ...

    DIRegEx v8.6.8 for D4-XE10 字符 匹配 文本编辑

    Matching and extraction of matches / substrings from the source text. Searching for regular expressions within streams and memory buffers. To search within streams or files (of virtually unlimited ...

    JavaProgramToFindAllSubstringsOfAString.pdf 英文原版

    Java Program To Find All Substrings Of A String

    DIRegEx v8.5.0 for D4-XE6 cracked

    Matching and extraction of matches / substrings from the source text. Searching for regular expressions within streams and memory buffers. To search within streams or files (of virtually unlimited ...

    linear-substrings:线性时间算法,用于查找任何给定String的任何子长度的词典上最后的子字符串

    线性子串给定任何字符串S,该算法将在O(n)时间内确定S的字典顺序上的最后一个子字符串,其中n = S的长度。 附加的功能: -给定任何长度L,以使0 &lt;= L &lt;= n,该算法将在O(n)时间内按字典顺序确定S的长度L的...

    【ASP.NET编程知识】.NET实用扩展方法详解.docx

    var length = source.Count(); if (startIndex || endIndex || startIndex &gt;= length) yield break; var index = 0; foreach (var item in source) { if (index &gt;= startIndex && index ) yield return item...

    Day4题解1

    这个题目涉及数学和质数生成。目标是找到一个范围内的所有数字,这些数字除以给定的两个数(a、b)的商是整数,且它们的质因数分解中不含已知的质数。主要知识点包括: 1. **质数生成**:`getprime`函数使用了...

    Python Power - The Comprehensive Guide (2008).pdf

    Good Selection of Tools Available ........................................................................12 Good Selection of Pre-built Libraries ........................................................

    LeetCode:Leetcode-解决方案

    ================ LeetCode ================动态编程1.... Palindromic Substrings [647]9. Maximum Length of Pair Chain [646]10. Integer Break [343]11. Count Numbers with Unique Digits [357]12. 2-Key

    more-itertools:除itertools之外,还有更多用于可迭代操作的例程

    分组, , , , , , , , , , , , ,前瞻和回顾间谍, 偷看, 可寻加窗windowed , 子字符串, substrings_indexes , 交错, windowed_complete , 成对增广count_cycle , 散布, 填充, mark_ends , ...

    leetcode卡-leetcode-weekly-contest-186:第186届LeetCode周赛

    substrings (i.e. left substring and right substring). The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. Example 1: Input: ...

Global site tag (gtag.js) - Google Analytics