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

题目链接:Codeforces 451D Count Good Substrings

题目大意:定义good string,就是就一个字符串的连续相同字符用一个该字符替代后,形成回文串的字符串。现在给出一个字符串,问说该字符串的子串中,为good string的串有多少个,分长度为奇数和偶数的输出。

解题思路:因为字符串的组成为a和b,所以只要是头尾相同的子串都是满足的。所以我们计算在奇数和偶数位置的奇数个数和偶数个数即可,然后用组合数学求出答案。

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

using namespace std;
typedef long long ll;
const int maxn = 1e5+5;

char str[maxn];

ll cal (ll u) {
    return u * (u-1) / 2;
}

int main () {
    ll oddA = 0, evenA = 0;
    ll oddB = 0, evenB = 0;
    scanf("%s", str);

    int len = strlen(str);
    for (int i = 0; i < len; i++) {
        if (str[i] == ‘a‘) {
            if (i&1)
                oddA++;
            else
                evenA++;
        } else {
            if (i&1)
                oddB++;
            else
                evenB++;
        }
    }

    ll even = oddA * evenA + oddB * evenB;
    ll odd = cal(oddA) + cal(evenA) + cal(oddB) + cal(evenB) + len;
    printf("%lld %lld\n", even, odd);
    return 0;
}

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

时间: 2024-10-08 13:29:32

Codeforces 451D 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

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个数. 那么到一个奇数点时,奇数长度的子串个数应该加上奇数位置的该字符的个数,偶数长度的应该加上偶

Count Good Substrings

Count Good Substrings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 451D64-bit integer IO format: %I64d      Java class name: (Any) We call a string good, if after merging all the consecutive equal

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

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

[Codeforces 1295F]Good Contest(DP+组合数学)

[Codeforces 1295F]Good Contest(DP+组合数学) 题面 有一个长度为\(n\)的整数序列,第\(i\)个数的值在\([l_i,r_i]\)中随机产生.问这个序列是一个不上升序列的概率(模\(998244353\)意义下). \(n \leq 50,l_i,r_i \leq 998244351\) 分析 和[APIO2016]划艇几乎一模一样.可惜比赛的时候时间不够. 首先把问题转化成求最长不上升序列的数量. 我们把这些区间离散化,分割成两两之间不互相覆盖的若干个区间

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