codeforces 451D Count Good Substrings
题意:给定一个字符串,求有多少个奇数子串和多少偶数子串为 “回文串” 这边回文串很特殊之含有 ab 两种字母 而且 相邻的字母相同则消去一个 一直到不存在相邻的相同。
思路: 在这种串中 ,消到最后 一定是 abababababa。。。 或者 bababababab。。。 那么 只要头尾一样 那么这个串 一定是 回文串。
那么 只需要 统计下 奇数位上 和 偶数位上a b个数就能直接计算。 一个在奇数位一个在偶数为 长度位偶数, 两个都在 奇数位 或者偶数位 则长度为奇数。
#include <iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define LL long long
#define f(x) ((x)*(x+1)/2)
using namespace std;
char s[100050];
int main() {
LL odda,oddb,evena,evenb;
odda=oddb=evena=evenb=0;
scanf(" %s",s);
int len=strlen(s);
for(int i=0;i<len;++i)
{
if(s[i]=='a')
{
if(i&1)
odda++;
else
evena++;
}
else
{
if(i&1)
oddb++;
else
evenb++;
} }
LL ans1=odda*evena+oddb*evenb;
LL ans2=f(odda)+f(oddb)+f(evena)+f(evenb);
printf("%I64d %I64d\n",ans1,ans2);
return 0;
}
codeforces 451D Count Good Substrings的更多相关文章
- codeforces D. Count Good Substrings
http://codeforces.com/contest/451/problem/D 题意:给你一个字符串,然后找出奇数和偶数长度的子串的个数,这些字串符合,通过一些连续相同的字符合并后是回文串. ...
- 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 ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- Codeforces 451D
题目链接 D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- Codeforces Round #258 D Count Good Substrings --计数
题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式 ...
- 【Codeforces 258D】 Count Good Substrings
[题目链接] http://codeforces.com/contest/451/problem/D [算法] 合并后的字符串一定是形如"ababa","babab&qu ...
随机推荐
- 从NSGA到 NSGA II
NSGA(非支配排序遗传算法).NSGAII(带精英策略的非支配排序的遗传算法),都是基于遗传算法的多目标优化算法,都是基于pareto最优解讨论的多目标优化,遗传算法已经做过笔记,下面介绍paret ...
- DDL、DML、DCL的理解
1.DDL 1-1.DDL的概述 DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,以 ...
- 马哥教育视频笔记:01(Linux常用命令)
1.查看缓存中使用的命令和命令路径 [wskwskwsk@localhost /]$ hash 命中 命令 /usr/bin/printenv /usr/bin/ls /usr/bin/clear 2 ...
- 关于JQ的$.deferred()
此文章是在网络上看到的. 一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比 ...
- mac攻略(二) -- 简单配置php开发环境
最简单直接的方式还是使用 Mac 上自带的 Apache 和 PHP. 1.启动 Apache 1>启动apache $sudo apachectl start; 2>启动后,在浏览器 ...
- document.compatMode属性和获取鼠标的位置
document.compatMode属性 document.compatMode用来判断当前浏览器采用的渲染方式. 官方解释: BackCompat:标准兼容模式关闭.CSS1Compat:标准兼容 ...
- centos6.6编译安装lnmp系列之PHP
简介: 环境:虚拟机+centos6.6 在百度云盘里面我分享的安装软件包,包含LNMP系列软件. http://pan.baidu.com/s/1gdvnimv 1.安装前配置: 关闭selinux ...
- 选出N个不重复的随机数
<script type="text/javascript"> var str="0123456789"; var arr=[]; var n; w ...
- 14.KVM安装之脚本和镜像目录树准备
1.php脚本需要先安装PHP环境,Apache服务器必须支持PHP $ yum install -y php #安装PHP $ php -v #查看是 ...
- Http 状态码对照表
1xx 消息 1. 100 Continue 2. 101 Switching Protocol 3. 102 Processing 2xx 成功 1. 200 OK ...