题目链接: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;
}

Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学的更多相关文章

  1. Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题

    D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...

  2. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  3. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

  4. Codeforces Round #258 (Div. 2)

    A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...

  5. Codeforces Round #258 (Div. 2)(A,B,C,D)

    题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...

  6. Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

    http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...

  7. Codeforces Round #258 (Div. 2) D

    D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. Codeforces Round #258 (Div. 2) B. Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  9. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

随机推荐

  1. zookeeper源码 — 四、session建立

    目录 session建立的主要过程 客户端发起连接 服务端创建session session建立的主要过程 用一张图来说明session建立过程中client和server的交互 主要流程 服务端启动 ...

  2. HTTPS协议工作流程

    被问到了,复习一下HTTPS的工作流程 提到https,不得不提SSL SSL 1.        安全套接字(Secure Socket Layer,SSL)协议是Web浏览器与Web服务器之间安全 ...

  3. 阿里云云服务器ubuntu配置nginx+uwsgi+django记录文档

    1 安装ssh 1  sudo apt-get update 2  sudo apt-get install openssh-server 3  sudo ps -e |grep ssh  有sshd ...

  4. Caused by: java.io.FileNotFoundException: rmi_keystore.jks (没有那个文件或目录)

    解决方法:修改jmeter.properites: server.rmi.ssl.disable=true,关闭ssl功能 参考: https://blog.csdn.net/nielinqi520/ ...

  5. 线性回归,logistic回归分类

    学习过程 下面是一个典型的机器学习的过程,首先给出一个输入数据,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计,也被称为构建一个模型.就如同上面的线性 ...

  6. 如何提高NodeJS程序的运行的稳定性

    如何提高NodeJS程序运行的稳定性 我们常通过node app.js方式运行nodejs程序,但总有一些异常或错误导致程序运行停止退出.如何保证node程序的稳定运行? 下面是一些可以考虑的方案: ...

  7. 从零開始开发Android版2048 (二)获取手势信息

    今天是尝试開始Android版2048小游戏的第二天.在今天,我主要学习了怎样获取用户在屏幕滑动的手势,以及对布局进行了一些小小的完好. 获取用户操作的手势(比方向左滑.向右滑等)主要用到了Gestu ...

  8. 反射 type 的基本用法,动态加载插件

    这里介绍反射的简单实用 MyClass类 public class MyClass { public int Age { get; set; } public string Name { get; s ...

  9. apollo 消息分发源代码分析

    1.MessageDispatch消息分发信息 public static final byte DATA_STRUCTURE_TYPE = CommandTypes.MESSAGE_DISPATCH ...

  10. Cocos2d-x 3.0 打造一个全平台概念文件夹

    Cocos2d-x 3.0 打造一个全平台概念文件夹http:// www.eoeandroid.com/thread-328055-1-1.html