Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学
题目链接:http://codeforces.com/problemset/problem/451/D
2 seconds
256 megabytes
standard input
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.
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'.
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
bb
1 2
baab
2 4
babb
2 5
babaa
2 7
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 —— 组合数学的更多相关文章
- 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 ...
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- Codeforces Round #258 (Div. 2)
A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...
- 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 ...
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...
- Codeforces Round #258 (Div. 2) D
D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #258 (Div. 2) B. Sort the Array
题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...
- 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 ...
随机推荐
- poj 2104 K-th Number(主席树
Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 59058 Accepted: 20529 Case Time Limi ...
- 配置tomcat启动参数-Dfile.encoding=UTF-8后,IDEA控制台乱码
配置tomcat启动参数-Dfile.encoding=UTF-8后,IDEA控制台出现乱码 解决方法: 在idea的bin目录(如:D:\JetBrains\IntelliJ IDEA 2018.1 ...
- layui-时间选择器-时间范围选择
HTML: JS: layui.use(['laydate'],function{ }); start:就为你选择的开始日期; end:就为你选择的结束日期 此方式可选择任意范围的时间,时间格式可任意 ...
- Css实现一个菜单导航
提要:使用大div定位设置为relative,子div设置为absolute实现菜单下拉 实现代码: <!DOCTYPE html> <html lang="en" ...
- tomcat知识点
(1)使用线程池 Servlet引擎为每一个请求创建一个隔离的线程,分配这个线程给service()方法,在它执行完后移除这个线程.默认情况下,servlet引擎 为每一个请求创建新的线程.因为创 ...
- 线程安全-一个VC下多个网络请求
一.线程安全变量控制显示隐藏loading框 问题描写叙述: 同一页面有两个异步网络请求,第一个请求開始,loading旋转.第二个请求開始loading旋转.第一个结束,loading停止旋转,但是 ...
- Oracle Apex 有用笔记系列 6 - 可编辑交互报告 Editable Interactive Report
据笔者所知.Apex 4.x 是没有提供可编辑交互报告组件的.这就须要我们手动实现. 事实上这也并非非常复杂,仅仅须要简单几步. 1. 依据向导建立一个interactive report.查询语句能 ...
- 整合Hibernate3.x
As of Spring 3.0, Spring requires Hibernate 3.2 or later. Hibernate 3和Hibernate 4有一些区别,所以对于spring而已, ...
- 利用display属性写出表格的布局样式
demo地址:http://codepen.io/tianzi77/pen/gpBzjy 元素结构: <h1>display构造的table小样例,IE8及下面浏览器不支持本演示样例< ...
- 2016年第七届蓝桥杯c/c++省赛B组
2016年第七届蓝桥杯c/c++省赛B组 声明:以下答案是我自己做的.不能保证正确,须要參考正确答案的请到其它地方找. 第一题 :煤球数目 题目叙述: 有一堆煤球,堆成三角棱锥形.详细: 第一层放1个 ...