CodeForces 451D Count Good Substrings

哎,最近都在做图论,没有练DP,现在一遇到DP就不会了= =

因为有合并这个操作,所以只要是首位相同的字符串肯定是能够构成good串的,那么只要统计在奇数位上出现的0,1的个数和偶数位数,随便递推一下就出来了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 100005;
LL cntodd[2],cnteven[2];
char buf[maxn];

int main() {
    scanf("%s",buf + 1);
    int len = strlen(buf + 1);
    LL sumodd = 0,sumeven = 0;
    for(int i = 1;i <= len;i++) {
        int now = buf[i] - ‘a‘;
        if(i & 1) cntodd[now]++;
        else cnteven[now]++;
        if(i & 1) {
            sumodd += cntodd[now];
            sumeven += cnteven[now];
        } else {
            sumodd += cnteven[now];
            sumeven += cntodd[now];
        }
    }
    cout << sumeven << " " << sumodd << endl;
    return 0;
}

  

CodeForces 451D Count Good Substrings,布布扣,bubuko.com

时间: 2024-10-10 11:13:41

CodeForces 451D Count Good Substrings的相关文章

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

题目链接:Codeforces 451D Count Good Substrings 题目大意:定义good string,就是就一个字符串的连续相同字符用一个该字符替代后,形成回文串的字符串.现在给出一个字符串,问说该字符串的子串中,为good string的串有多少个,分长度为奇数和偶数的输出. 解题思路:因为字符串的组成为a和b,所以只要是头尾相同的子串都是满足的.所以我们计算在奇数和偶数位置的奇数个数和偶数个数即可,然后用组合数学求出答案. #include <cstdio> #inc

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

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

LeetCode算法题-Count Binary Substrings(Java实现)

这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串的数量,并且这些子串中的所有0和所有1都是连续的.重复出现的子串也计算在内.例如: 输入:"00110011" 输出:6 说明:有6个子串具有相同数量的连续1和0:"0011","01","1100","10"