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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- <每日一题>题目20:简单python练习题(11-20)
#11.编写程序,输入一个自然数,输出它的二进制.八进制.十六进制表示形式 Num = input("请输入任性自然数:") Num = eval(Num) print(" ...
- C#获取七牛云token/删除七牛云图片接口
// 获取七牛token public ApiResponse GetQiniuToken(QiniuToken req) { try { Mac mac = new Mac(req.AccessKe ...
- jQuery实现textarea高度根据内容自适应
//jQuery实现textarea高度根据内容自适应 $.fn.extend({ txtaAutoHeight: function () { return this.each(function () ...
- 跟我一起了解koa之koa-generator(一)
cnpm install -g koa-generator koa2 -e koa2-learn cd koa2-learn/ cnpm install 使用如下运行 DEBUG=koa2-learn ...
- CentOS 6.5 源码编译搭建LAMP(两台独立主机实现)
搭建前准备: 1.两台独立主机 httpd:192.168.1.105 php-fpm:192.168.1.105 mariadb:192.168.1.103 2.相关软件的源码包 httpd:htt ...
- csp-s模拟47 Emotional Flutter,Endless Fantasy题解
题面:https://www.cnblogs.com/Juve/articles/11558523.html A:Emotional Flutter 如果起点确定,那么我们后面走的点都是固定的,及mo ...
- Vue.nextTick()的介绍和使用场景
每次都很好奇这个干嘛的,然后百度之后还是不明白.今天就彻彻底底好好的弄明白这是干嘛的!! 首先看一下vue文档 nextTick(),是将回调函数延迟在下一次DOM更新数据后调用,简单的理解是:当数据 ...
- (二)通过JAVA调用SAP接口 (增加一二级参数)
(二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...
- tensorflow高效地推导pb模型,完整代码
from matplotlib import pyplot as plt import numpy as np import os import six.moves.urllib as urllib ...
- Vuejs实战项目:登陆页面
1.在view文件夹下创建login文件夹,创建index.vue:代表登录的组件 配置router.js,导入登录组件 import Vue from "vue"; import ...