codeforces Round #258(div2) D解题报告_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:55:19
原创
1387人浏览过

猫眼课题宝
猫眼课题宝

5分钟定创新选题,3步生成高质量标书!

猫眼课题宝 85
查看详情 猫眼课题宝

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。总个数很容易在O(n)的时间复杂度求出来,但我们现在要求的是长度为奇数和偶数的个数,也就是拆开来,那我们就可以将求总个数的方法,拆一下。

位置为奇数的字符,与位置为偶数的字符构成偶数长度的good substrings,

                                与位置为奇数的字符构成奇数长度的good substrings,

位置为偶数的字符,与位置为偶数的字符构成奇数长度的good substrings,

                                 与位置为奇数的字符构成偶数数长度的good substrings,

代码:

#include <string>#include <iostream>#define LL long longusing 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();}
登录后复制

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号