题目链接

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. PAT甲级——A1082 Read Number in Chinese

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...

  2. EF中的DbContext类

    使用先前的数据上下文,可以通过使用LINQ查询,按字母顺序检索出所有专辑,代码如下

  3. tiger-complier 问题记录 类型检查

    1 注意区分 Ty_xxx 和 a_ 的区别 A 开头是对应的是原程序中的一个实体 Ty _ 开头则是类型检查中的一个抽象类 代表了一类实体 比如 A_recordTy 是原程序中的 type per ...

  4. MacOS 读写 NTFS 即插即用.

    1. 安装osxfusehttps://osxfuse.github.io/ 2. 安装brewhttps://brew.sh/index_zh-cn.html 3. 安装ntfs-3gbrew in ...

  5. [转]WPF中的动画

    WPF中的动画                                                                                  周银辉 动画无疑是WP ...

  6. 阿里云数据库再获学术顶会认可,一文全览VLDB最新亮点

    一年一度的数据库领域顶级会议VLDB 2019于当地时间8月26日-8月30日在洛杉矶圆满落幕.在本届大会上,阿里云数据库产品团队浓墨登场,不仅有多篇论文入选Research Track和Indust ...

  7. 关于JEECMS套站工具的使用要点

      第一步:在[界面—资源]下面引入资源文件(js,css,img…) 第二步:在[界面—模板]下面将网站的入口页面写在[index]文件下 此时修改index页面中的 js,css,图片 的路径,路 ...

  8. anaconda安装使用

    Conda是一个开源的包.环境管理器,可以用于在同一个机器上安装不同版本的软件包及其依赖,并能够在不同的环境之间切换 下载:https://mirrors.tuna.tsinghua.edu.cn/a ...

  9. redis学习笔记05-发布订阅模式

    由于redis消息队列并不支持多播机制,即生产者产出一次,由中间件复制发送给多个消费者的机制,为了弥补这个不足,单独引入了发布订阅模式的模块.使用方式如下: #生产者 import redis cli ...

  10. xshell下载免费版

    正在使用的xshell 5不能使用,提示xshell 5评估期已过,重新安装还是不行.其实xshell 5有免费版的,即Home & school 版本.卸载原程序,下载安装免费版本的xshe ...