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 ...
随机推荐
- MySQL 5.6 SQL 优化及 5.6手册
http://blog.chinaunix.net/uid-259788-id-4146363.html http://www.cnblogs.com/Amaranthus/p/4028687.htm ...
- ThreadLocal 笔记
synchronized 同步的机制可以解决多线程并发问题,这种解决方案下,多个线程访问到的都是同一份变量的内容.为了防止在多线程访问的过程中,可能会出现的并发错误.不得不对多个线程的访问进行同步,这 ...
- 微信分组群发45028,微信分组群发has no masssend quota hint
微信分组群发45028,微信分组群发has no masssend quota hint >>>>>>>>>>>>>> ...
- 程序员带你十天快速入门Python,玩转电脑软件开发(四)
本系列文章立志于从一个已经习得一门编程语言的基础之上,全面介绍Python的相关开发过程和相关经验总结.本篇文章主要是基于上一篇的程序员带你十天快速入门Python,玩转电脑软件开发(三)的基础之上, ...
- WebStrom安装了angularjs插件,但是没有语法提示
你必须要将angular.js文件放在当前工程下才能激活代码提示功能.
- Js 的常用方法:页面跳转,Session,类继承
MyApp.Base = function () { } var basePrototype = MyApp.Base["prototype"]; //对象克隆方法 basePro ...
- C#当中的多线程_线程基础
前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...
- centos 彻底卸载mysql
yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...
- 类库探源——System.Delegate
一.MSDN 描述 Delegate 类:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法.(是不是感觉很像C语言中的函数指针 :) ) 命名空间: System 程序集: ...
- ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...