题目链接

D. Count Good Substrings
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".

Given a string, you have to find two values:

  1. the number of good substrings of even length;
  2. the number of good substrings of odd length.
Input

The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.

Output

Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.

Sample test(s)
input
bb
output
1 2
input
baab
output
2 4
input
babb
output
2 5
input
babaa
output
2 7
Note

In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length.

In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length.

In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length.

Definitions

A substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.

A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1.

这题只需要保存当前位置为止,在奇数位上和偶数位上分别出现了多少次0和1。

从来没有使用过python的二维数组。。。在别人的代码中学习了,不过还是不会用。。

如:

c = [[0 for i in xrange(2)] for i in xrange(2)]

或者:

 a = {'a' : [0 , 0] , 'b' : [0 , 0]}

Accepted Code:

 s = list(raw_input());
L = len(s);
even, odd = 0, 0;
ev, od = [0]*2, [0]*2;
for i in xrange(1, L+1):
t = 0 if s[i-1]=='a' else 1;
if i % 2 :
od[t] += 1
even += ev[t];
odd += od[t]
else :
ev[t] += 1
even += od[t]
odd += ev[t]
print even, odd

Codeforces 451D的更多相关文章

  1. codeforces 451D Count Good Substrings

    题意:给定一个字符串,求有多少个奇数子串和多少偶数子串为 “回文串”   这边回文串很特殊之含有  ab  两种字母  而且  相邻的字母相同则消去一个  一直到不存在相邻的相同. 思路:  在这种串 ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. jdom xpath定位带xmlns命名空间的节点(转)

    jdom xpath定位带xmlns命名空间的节点 2013-06-29      0个评论       作者:baozhengw 收藏    我要投稿 关键词:jdom xpath xmlns 命名 ...

  2. SPRINGBOOT配置事物注解和@MAPPER注意

    MAPPER接口要使用@Mapper注解,不能用@Compent @Repository,否则没有效果 一.开启事物 在启动类上加 @EnableTransactionManagement //如果m ...

  3. python configparser模块详解

    此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...

  4. layui 表格点击图片放大

    表格 ,cols: [[ //表头 {checkbox: true,fixed: true} ,{type: 'numbers', title: 'ID', sort: true,width:80} ...

  5. C# Predicate委托

    Predicate在集合搜索和WPF数据绑定中用途广泛,其调用形式: 调用形式:Predicate<object>(Method)/Predicate<参数类型>(方法) 1. ...

  6. js的相关距离

    js的相关距离 一.dom对象的距离 ---dom.style.width : 对象本身的内容宽度(这里必须是内联样式中的width,带px)(content) ---dom.style.height ...

  7. fileinput使用心得

    下咋以及一些具体使用过程就不叙述了,简单说一下使用时候需要注意的几点 1.在js中封装好的fileinput函数 /* * 初始化fileInput控件(第一次初始化) * type 不同类别 * i ...

  8. HTMl中常用标签

    文本标记语言,即HTML(Hypertext Markup Language),是用于描述网页文档的一种标记语言. HTML之所以称为超文本标记语言,是因为文本中包含了所谓“超级链接”点.所谓超级链接 ...

  9. PAT甲级——A1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  10. $(...).live is not function

    项目中引入了一个插件,但是调用的时候就报了$(...).live is not function 上网搜索了一下live方法在1.9中被删除了,因为平时自己用的时候就用on的方法,没用过live,所以 ...