codeforces Round #258(div2) D解题报告
2 seconds
256 megabytes
standard input
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:
- the number of good substrings of even length;
- the number of good substrings of odd length.
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'.
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
bb
1 2
baab
2 4
babb
2 5
babaa
2 7
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.
题目大意:
给出一串字符,仅仅含有a和b。如今定义一个字串如若合并之后的字串是个回文字符串,就是一个good substrings,求出这种字串有多少个。并输出长度为偶数和奇数的个数。
解法:
首先,我们须要注意到两个已知条件:
1. 字串能够合并。比如 abbaabbb 合并之后就是abab
2. 仅仅有两个字符a。b
我们能够发现,合并之后的字串一定是aba或者abab类型的,那么合并之后的字串假设是回文的话,第一个字符肯定与最后一个字符同样,反之亦然。
我们能够进一步得出一个结论:两个不同的位置,字符同样,之间构成的字串一定是good substrings。
总个数非常easy在O(n)的时间复杂度求出来,但我们如今要求的是长度为奇数和偶数的个数,也就是拆开来,那我们就能够将求总个数的方法。拆一下。
位置为奇数的字符。与位置为偶数的字符构成偶数长度的good substrings,
与位置为奇数的字符构成奇数长度的good
substrings,位置为偶数的字符。与位置为偶数的字符构成奇数长度的good
substrings,与位置为奇数的字符构成偶数数长度的good substrings,
代码:
#include <string>
#include <iostream>
#define LL long long using namespace std; string st;
LL ansOdd, ansEven;
LL Odd[2], Even[2]; void init() {
cin >> st;
} void solve() {
for (int i = 0; i < st.size(); i++) {
ansOdd++;
int j = st[i]-'a'; if ((i+1)&1) {
ansOdd += Odd[j];
ansEven += Even[j];
Odd[j]++;
}
else {
ansEven += Odd[j];
ansOdd += Even[j];
Even[j]++;
}
} cout << ansEven << ' ' << ansOdd << endl;
} int main() {
init();
solve();
}
codeforces Round #258(div2) D解题报告的更多相关文章
- codeforces Round #258(div2) C解题报告
C. Predict Outcome of the Game time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- codeforces Round #259(div2) C解题报告
C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- Codeforces Round #380 (Div. 2) 解题报告
第一次全程参加的CF比赛(虽然过了D题之后就开始干别的去了),人生第一次codeforces上分--(或许之前的比赛如果都参加全程也不会那么惨吧),终于回到了specialist的行列,感动~.虽然最 ...
- Codeforces Round #281 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/493 A题 写完后就交了,然后WA了,又读了一遍题,没找出错误后就开始搞B题了,后来回头重做的时候才发现,球员被红牌罚下场后还可 ...
- Codeforces Round #277 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...
- Codeforces Round #276 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...
- Codeforces Round #350 (Div. 2)解题报告
codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...
随机推荐
- Jquery图片随滚动条加载
很久以前的写的Jquery图片随滚动条加载,现在不是什么新技术,应用也很广泛,大大提高图片多的页面打开速度! 有需要的朋友看看吧!有什么意见或建议欢迎留言交流! Demo.html 源码: < ...
- unity3d Human skin real time rendering 真实模拟人皮实时渲染(转)
先放出结果图片...由于网上下的模型是拼的,所以眼皮,脸颊,嘴唇看起来像 存在裂痕,解决方式是加入曲面细分和置换贴图 进行一定隆起,但是博主试了一下fragment shader的曲面细分,虽然细分成 ...
- vim中编码方式---不完整总结
关于编码,总有很多故事,这里只是记录下暂时的东西. 1.关于查看文件的编码 在查看文件编码的时候,总是倾向于使用file来进行查看,然而使用file命令的时候,并没有什么卵用: 在查看细节的时候,可以 ...
- Oracle 游标使用(转)
这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 ; ; dbms_output.put_line(sql) loop dbms_output.put_line( ; ; ; r_te ...
- 第三百二十四天 how can I 坚持
下午去打了会篮球,好累,又把android开发环境搭建起来了,明天把天气应用搞起来. 今天老妈打电话说昨晚梦到我小时候了.. 是啊,都这么大了,不能让他们老操心了. 过两天买根鱼竿去钓鱼. 睡觉.
- MYSQL数据库重点:事务与锁机制
一.事务 一组连续的数据库操作,每一次操作都成功,整个事务就成功,只要有一步出错,整个事务就失败: MySQL事务与存储引擎相关 1.MyISAM:不支持事务,用于只读程序提高性能 2.InnoDB: ...
- 安装Sass
最近要开始用 Sass 做一些东西.先来记录一下安装过程. 1.确认本机的 Ruby 版本 2.访问网址下载 Sass 最新版本 https://rubygems.org/gems/sass 3.下载 ...
- webconfig文件serviceHostingEnvironment节点出错的解决方法
在三点五和二版本的配置中可以出现这个节点,但是在4.0是没有的,所以如果框架是4.0的时候要除去这个节点,不然就会报以下错误: Configuration Error Description: An ...
- Linux下安装protobuf并实现简单的客户端服务器端通信
http://code.google.com/p/protobuf/downloads/list上可以下载Protobuf的源代码. 安装步骤如下所示: 1>tar -xzf protobuf- ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...