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

sl :分析发现最后回文串的第一个字符和第二个字符相同,这样统计相应数位上的字符就行了。

 1 //by caonima
 2 //hehe
 3 #include <bits/stdc++.h>
 4 typedef long long LL;
 5 const int MAX = 1e5+;
 6 char str[MAX];
 7 LL even_cnt[],odd_cnt[];
 8 // odd ji even o
 9 int main() {
     LL odd,even;
     while(scanf("%s",str+)==) {
         memset(even_cnt,,sizeof(even_cnt));
         memset(odd_cnt,,sizeof(odd_cnt));
         int n=strlen(str+);
         odd=even=;
         for(int i=;i<=n;i++) {
             int x=str[i]-'a';
             if(i&) {
                 odd+=odd_cnt[x];
                 even+=even_cnt[x];
                 odd_cnt[x]++;
             }
             else {
                 odd+=even_cnt[x];
                 even+=odd_cnt[x];
                 even_cnt[x]++;
             }
         }
         odd+=n;
         printf("%I64d %I64d\n",even,odd);
     }
     return ;

33 }

Codeforces Round #258 (Div. 2) D的更多相关文章

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

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

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

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

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

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

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

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

  6. Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game 水题

    C. Predict Outcome of the Game 题目连接: http://codeforces.com/contest/451/problem/C Description There a ...

  7. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  8. Codeforces Round #258 (Div. 2) A. Game With Sticks 水题

    A. Game With Sticks 题目连接: http://codeforces.com/contest/451/problem/A Description After winning gold ...

  9. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  10. Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学

    题目链接:http://codeforces.com/problemset/problem/451/D D. Count Good Substrings time limit per test 2 s ...

随机推荐

  1. Service官方教程(10)Bound Service的生命周期函数

    Managing the Lifecycle of a Bound Service When a service is unbound from all clients, the Android sy ...

  2. 定时清除 /var/log/massage 下的信息脚本文件

    定时清除 /var/log/massage 下的信息脚本 #!/bin/sh #Date: 0:07 #Author: Xiaodong #Mail: 990974238@qq.com #Puncti ...

  3. Ubuntu编译安装Apache

    第一步:编译安装依赖包apr,apr-util和pcre ./configure --prefix= make && make install 第二步:编译安装Apache ./con ...

  4. SPFarm.local返回值为null

    创建了一个控制台应用程序,想输出SP2010服务器场下所有对象模型信息,结果:SPFarm.local返回值为null. 经查询解决方法: 1 .net framework版本要使用3.5: 2 目标 ...

  5. 关于java的arrays数组排序示例AJPFX的分享

    Java API对Arrays类的说明是:此类包含用来操作数组(比如排序和搜索)的各种方法. 1.对基本数据类型的数组的排序 说明: (1)Arrays类中的sort()使用的是“经过调优的快速排序法 ...

  6. CF916C Jamie and Interesting Graph

    思路:构造 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n ...

  7. 快速开发框架天梭(Tissot)

    天梭(Tissot)集成SpringBoot+Dubbo等主流互联网技术栈,高度集成.优化方便快速搭建应用.某互金科技公司内部孵化框架,已应用于公司90%业务系统. 框架划分模块有: tissot-c ...

  8. 到T-SQL DML 三级的阶梯:在SQL server中实现关系模型

    作者: Gregory Larsen, 2017/08/02 (第一次出版: 2011/11/09) 翻译:谢雪妮,许雅莉,赖慧芳,刘琼滨 译文: 系列 该文章是阶梯系列的一部分:T-SQL DML的 ...

  9. 仿陌陌的ios客户端+服务端源码

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...

  10. mathAge.call(btn) 函数call 改变函数内 this #js

    mathAge.call(btn) 函数call 改变函数内 this