题目链接:http://codeforces.com/problemset/problem/451/D

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.

Examples
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.

题解:

1.分别记录‘a’在奇数位置、偶数位置出现的次数, ‘b’亦如此。

2.长度为偶数的情况:相同的字母一个出现在奇数位置, 一个出现在偶数位置,假设出现次数分别为n、m, 则n*m。

3.长度为偶数的情况:相同的字母都出现在偶数位置或者奇数位置。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e5+10; char s[maxn];
int a[2][2]; LL f(int x) { return 1LL*x*(x-1)/2; } int main()
{
scanf("%s",s+1);
int len = strlen(s+1);
for(int i = 1; i<=len; i++)
a[s[i]!='a'][i&1]++; LL even = 1LL*a[0][0]*a[0][1] + 1LL*a[1][0]*a[1][1];
LL odd = f(a[0][0]) + f(a[0][1]) + f(a[1][0]) + f(a[1][1]) + len;
cout<<even<<' '<<odd<<endl;
}

最新文章

  1. vmware rdm
  2. react-native 调用 TouchableOpacity (触摸透明) 时报了一个警告
  3. jquery 无缝滚动 jquery.kxbdmarquee
  4. [转]Spring 注解总结
  5. NHibernate 中删除数据的几种方法
  6. EasyUI_Datagrid学习总结
  7. Java集合框架类图
  8. Hadoop core-site.xml 配置项列表
  9. window.onload 和 $(document).ready(function(){})的区别
  10. Spring的事务管理(理论篇,下篇提供代码实现)
  11. POST方式提交乱码解决
  12. pta总结3
  13. Python3.* 和Python2.*的区别
  14. Docker 二进制安装docker
  15. H5开发:横屏适配
  16. Expect 安装 on centos7
  17. Python中的正则表达式探秘1
  18. Clipboard Action for Mac(智能剪贴板历史管理器)破解版安装
  19. 同一个脚本在SQLPLUS和SQLDEV上的不同
  20. TlistView基本使用

热门文章

  1. android清除缓存为什么总是存在12k?
  2. iOS开发之分段控制器(UISegmentedControl)
  3. nyoj 1077 小博弈 【另类巴什博奕】
  4. 百科知识 .tar.xz文件如何打开
  5. C++11 并发指南四(&lt;future&gt; 详解一 std::promise 介绍)(转)
  6. 关于mysql的表名/字段名/字段值是否区分大小写的问题
  7. 【转载】C#中的==、Equal、ReferenceEqual
  8. 函数指针使用演示样例(參考Linux-内核代码)
  9. PHP魔术方法之__call与__callStatic方法
  10. Html调用 QQ接口