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.

题目大意:

给出一串字符,仅仅含有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解题报告的更多相关文章

  1. codeforces Round #258(div2) C解题报告

    C. Predict Outcome of the Game time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. codeforces Round #259(div2) C解题报告

    C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  3. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  4. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  5. Codeforces Round #380 (Div. 2) 解题报告

    第一次全程参加的CF比赛(虽然过了D题之后就开始干别的去了),人生第一次codeforces上分--(或许之前的比赛如果都参加全程也不会那么惨吧),终于回到了specialist的行列,感动~.虽然最 ...

  6. Codeforces Round #281 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/493 A题 写完后就交了,然后WA了,又读了一遍题,没找出错误后就开始搞B题了,后来回头重做的时候才发现,球员被红牌罚下场后还可 ...

  7. Codeforces Round #277 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...

  8. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  9. Codeforces Round #350 (Div. 2)解题报告

    codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...

随机推荐

  1. LoadRunner界面分析(二)

    1.Controller 2.创建运行场景 3.方案设计 4.Resuls settting 5.监视方案

  2. Google搜索的常用技巧

    个人搜索方案 1.选择合适的搜索词,一些行业术语或专家名字可以带来更加高质量的结果. 2.搜索词手动使用空格分隔,先进行第一次搜索,看搜索结果标题是否满足预期,如果不满足,采用更换关键词,添加关键词, ...

  3. IoC Service Provier

    本文节选自<Spring 揭秘>. 虽然业务对象可以通过IoC方式声明相应的依赖,但是最终仍然需要通过某种角色或者服务将这些相互依赖的对象绑定到一起,而IoC Service Provid ...

  4. 使用Python拼接多张图片

    写机器学习相关博文,经常会碰到很多公式,而Latex正式编辑公式的利器.目前国内常用的博客系统,好像只有博客园支持,所以当初选择落户博客园.我现在基本都是用Latex写博文,然后要发表到博客园上与大家 ...

  5. Chapter9:顺序容器

    现代C++程序应该使用标准库容器,而不是更原始的数据结构,例如内置数组. 新标准库容器的性能几乎肯定与最精心优化过的同类数据结构一样好. 当我们用一个对象来初始化容器时,或将一个对象插入到容器中时,实 ...

  6. 认识Agile,Scrum和DevOps

    If everything's under control you are going too slow. 当今的开发,要求faster and faster.所以我们要Agile,become Ag ...

  7. @Html.Raw()

    asp.net mvc中把html字符串以html效果输出来, @string变更输出的是HTML代码, 如果想以HTML标签效果输出来可以用函数@Html.Raw(str) 输出来的就是网效果了, ...

  8. 【boost】使用lambda表达式和generate_n生成顺序序列

    程序中经常用到顺序序列(0,1,2,3,4,5,6.....),一直羡慕python有range这样的函数,而C++中通常只有用循环来处理这种初始化. 现在,结合boost库lambda(虽然差C++ ...

  9. Android 相关

    ADT 下载更新 http://www.oschina.net/question/1463998_220998 更改包名后,资源文件不更新 AndroidMainfast.xml文件,有package ...

  10. SQL时间第一期_获取系统年月日时分秒

    select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...