codeforces Round #258(div2) D解题报告
2016-04-09 18:34
453 查看
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:
the number of good substrings of even length;
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
output
input
output
input
output
input
output
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,
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:
the number of good substrings of even length;
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(); }
相关文章推荐
- android性能调优工具TraceView的使用
- DOM
- 第七周项目一(2) 求两点间的距离
- VB.net版机房收费系统——结账功能实现(调错与优化)
- 骨骼的一点东西
- 受限波兹曼机导论Introduction to Restricted Boltzmann Machines
- 练习二1005
- Hibernate 关联映射之---- 一对多双向映射
- Java实验2 类的继承性
- caffe的配置过程
- linux常用操作
- 目标特征检测之SIFT特征
- java.lang.ExceptionInInitializerError的原因
- 使用Bitmap遍历Assets目录下图片
- 一些蓝牙(Bluetooth)相关的技术术语表
- 关于迭代器中IEnumerable与IEnumerator的区别
- Android 中View的绘制机制源代码分析 三
- 基于Selenium的模拟浏览器采集
- Getting Started with C++ in Visual Studio
- 点集的视点特征直方图的评估