Codeforces 451D
D. Count Good Substringstime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:
- the number of good substrings of even length;
- the number of good substrings of odd length.
InputThe 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'.
OutputPrint two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
Sample test(s)inputbboutput1 2inputbaaboutput2 4inputbabboutput2 5inputbabaaoutput2 7NoteIn 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的更多相关文章
- codeforces 451D Count Good Substrings
题意:给定一个字符串,求有多少个奇数子串和多少偶数子串为 “回文串” 这边回文串很特殊之含有 ab 两种字母 而且 相邻的字母相同则消去一个 一直到不存在相邻的相同. 思路: 在这种串 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- PAT甲级——A1007 Maximum Subsequence Sum
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- Maven实战08_仓库
何为Maven仓库 在Maven世界中.任何一个依赖.插件或者项目构建的输出,都可以称之为构件.例如依赖log4j-1.2.15.jar是一个构件,差价maven-compile-plugin-2.0 ...
- Python ----键抠图
背景 这段时间,经常有人来找我,说我是学计算机的,能不能帮他p一下证件照,我只想说,MMP的,我是学计算机的不错,可我不会ps阿. 我想了一会,python 这么火,能不能来个自动抠图,说好就干吧 介 ...
- Python Flask学习之安装SQL,python3,Pycharm(网上下载安装即可)
1,下载时更改pypi源.可以额外安装虚拟化环境:pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.co ...
- app-safeguard-record:record
ylbtech-work-app-safeguard-record:record 1.返回顶部 1. 示数 示数一般是指机械.仪器.仪表.或者需要对数字进行公开的显示的对外数字的宣示.比如电度表(千瓦 ...
- Java中"str1.equals(str2)"和"str1==str2"的区别
大家好,这是我的第一篇博客,作为即将入职的学生,我现在的心情是既好奇又兴奋,对未知的职场生活充满了无限的憧憬,也想赶紧对大学生活say goodbye,因为自己的能力现在还比较有限,我想通过博客这个平 ...
- java 异常捕获小记
java 中异常捕获常用的为: try{ //业务代码 }catch(Exception e){ //异常捕获 }finally{ // 不管有无异常, 最后都会执行到这里 } 在方法体内如果想要把异 ...
- 二、Python安装和第一个程序
<1.Python语言介绍1.官方介绍:Python 是一款易于学习且功能强大的编程语言. 它具有高效率的数据结构,能够简单又有效地实现面向对象编程.Python 简洁的语法与动态输入之特性,加 ...
- 关于apache kylin 安装32位linux办法
最近公司在使用apache kylin做实时开发访问,但是自己集群是linux32 只能安装JDK32位的受限只能3G多内存,而apachekylin 默认是4G内存,需要JDK64位支持. 解决办法 ...
- vim编辑shell
vi编辑 u撤销 i输入 dd删除游标所在的那一整行(常用) yy复制游标所在的那一行(常用) p 为将已复制的数据在光标下一行贴上 nyy n 为数字.复制光标所在的向下 n 行,例如 20yy ...