Count Good Substrings
Codeforces Round #258 (Div. 2) D:http://codeforces.com/problemset/problem/451/D
题意:给你一个字符串,只有ab组成。相同的字符可以消除一个,如果最终的串是一个回文串,那么原来的串就是一个good string.问长度为奇数和偶数的串各有多少个。
题解:只要观察到,这样的串开头的字母和最后一个字母是相同的,那么题目就简单了。我们只要从左到右扫一遍就可以了。统计之前出现过的偶位数上a有多少。奇数位上a有多少,偶数位上b有多少,奇数位上b有多少,对于当前位是a,如果这一位是偶数,那么奇数串的个数就是加上之前偶数位的a的个数,偶数串的个数就是加上之前奇数位上a的个数,其他的情况类似得到。然后更新偶数位a的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
char str[N];
long long odda,oddb;
long long evena,evenb;
long long ans1,ans2;
int main(){
scanf("%s",str);
int n=strlen(str);
odda=oddb=evena=evenb=ans1=ans2=;
for(int i=;i<n;i++){
if(str[i]=='a'){
if((i+)&){
ans1+=odda;
ans2+=evena;
odda++;
}
else{
ans1+=evena;
ans2+=odda;
evena++;
}
}
else {
if((i+)&){
ans1+=oddb;
ans2+=evenb;
oddb++;
}
else{
ans1+=evenb;
ans2+=oddb;
evenb++;
}
}
}
printf("%I64d %I64d\n",ans2,ans1+n);
}
Count Good Substrings的更多相关文章
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题
D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...
- Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学
题目链接:http://codeforces.com/problemset/problem/451/D D. Count Good Substrings time limit per test 2 s ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [LeetCode&Python] Problem 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
随机推荐
- 字符串匹配算法-BM
在用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广泛的一种算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用Boyer-Moore算法.比我们学习的KMP算 ...
- WTL 自绘 进度条Progressbar
WTL 绘制的进度条,逻辑清晰明了,代码函数清晰易懂:基本思路就是 首先绘制 进度条背景图,然后根据动态进度不断重绘前景进度条,绘制操作在OnPaint函数里画.该类可以直接用于项目中. 使用示例: ...
- 输入一个字符串,去掉重复的字符,并按ASCII值排序-华为机试
import java.util.Scanner; //输入字符串,去掉重复的字符,并按ASSIC码值排序 public class quChong { public static void main ...
- 【Android】知晓当前是哪一个活动
首先需要新建一个 BaseActivity 继承自Activity,然后在 BaseActivity 中重写 onCreate()方法,如下所示:public class BaseActivity e ...
- servlet登录
package com.lxr.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Pre ...
- Base64的Java代码实现
欢迎拍砖~ 在数据二进制和byte互相转换的地方方法写得有点挫,不知道有没有更好的方法~ 顺便复习了java的一些基础东西,如位操作,原码反码补码 可以在这篇blog里学习到详细的知识点:http:/ ...
- python基础知识十
特殊的方法 在类中有一些特殊的方法具有特殊的意义,比如__init__和__del__方法,它们的重要性我们已经学习过了. 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[k ...
- Java验证码代码
public class VerifyCodeController { private int width = 90;//定义图片的width private int height = 20;//定义 ...
- 用原生js实现一个页面乘法口诀表
今天我自己用js实现了一个页面乘法口诀表(如图)来共享给大家,做的不是很好,如果大家有新的想法可以跟我交流哦. 代码如下: <!doctype html><html lang=&qu ...
- 从网页psd到html的开发
从网上下载了一张psd的网页设计稿,初学html+css,所以记录一下我的学习过程.原图是这个样子: 原图 ...