Codeforces Round #258 (Div. 2) D:http://codeforces.com/problemset/problem/451/D

题意:给你一个字符串,只有ab组成。相同的字符可以消除一个,如果最终的串是一个回文串,那么原来的串就是一个good string.问长度为奇数和偶数的串各有多少个。

题解:只要观察到,这样的串开头的字母和最后一个字母是相同的,那么题目就简单了。我们只要从左到右扫一遍就可以了。统计之前出现过的偶位数上a有多少。奇数位上a有多少,偶数位上b有多少,奇数位上b有多少,对于当前位是a,如果这一位是偶数,那么奇数串的个数就是加上之前偶数位的a的个数,偶数串的个数就是加上之前奇数位上a的个数,其他的情况类似得到。然后更新偶数位a的个数。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
char str[N];
long long odda,oddb;
long long evena,evenb;
long long ans1,ans2;
int main(){
scanf("%s",str);
int n=strlen(str);
odda=oddb=evena=evenb=ans1=ans2=;
for(int i=;i<n;i++){
if(str[i]=='a'){
if((i+)&){
ans1+=odda;
ans2+=evena;
odda++;
}
else{
ans1+=evena;
ans2+=odda;
evena++;
}
}
else {
if((i+)&){
ans1+=oddb;
ans2+=evenb;
oddb++;
}
else{
ans1+=evenb;
ans2+=oddb;
evenb++;
}
}
}
printf("%I64d %I64d\n",ans2,ans1+n);
}

Count Good Substrings的更多相关文章

  1. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

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

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

  4. 【Leetcode_easy】696. Count Binary Substrings

    problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...

  5. 696. Count Binary Substrings - LeetCode

    Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...

  6. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  7. [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  8. LeetCode 696 Count Binary Substrings 解题报告

    题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...

  9. 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  10. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

随机推荐

  1. hibernate相关知识

    1.为什么要用Hibernate JDBC的优点 直接底层操作,提供了很简单.便捷的访问数据库的方法,跨平台性比较强.灵活性比较强,可以写很复杂的SQL语句. JDBC的缺点 因为JAVA是面向对象的 ...

  2. JAVA格式化时间日期

    JAVA格式化时间日期 import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * ...

  3. android自定义控件之滚动广告条

    在一些电子商务网站上经常能够看到一些滚动的广告条,许多软件在首次使用时也有类似的广告条,如图: 其实在github上有实现这种效果的控件,不过这东西做起来也是很简单,我们今天就来看看该怎么做. 先来看 ...

  4. WPF 格式化输出- IValueConverter接口的使用

    以前在用ASP.NET 做B/S系统时,可以方便地在GRIDVIEW DATAList等数据控件中,使用自定义的代码逻辑,比如 使用 <%# GetBalance(custID) %> 这 ...

  5. PHPStorm&PHPstudy环境配置

    因为实习要求,最近在学php,补下开发环境的配置,原博客链接:点击打开链接 1.创建新的项目(project),创建完成之后单击工具栏的应用运行/调试(Select Run/Debug Configu ...

  6. php 这门语言

    1,基本语法 php在解析一个文件时,会查找开始和结束标记,在开始标记和结束标记之外的会被php引擎忽略 注释:使用 // 和 /*这里是注释*/ 2,php 数据类型 整形 (2345) 浮点型(3 ...

  7. Devexpress 使用经验 —— ASPxGridView前后台交互写法推荐

    这里的格式是仁者见仁智者见智,这篇随笔只是我在工作过程中总结出的阅读性高,对我来说效率较高的写法. ASPX: <dx:ASPxGridView ID="ASPxGridViewLin ...

  8. declare-styleable:自定义控件的属性

    http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html 在使用过程中, 1 TypedArray a = getContext() ...

  9. js获取页面元素距离浏览器工作区顶端的距离

    先介绍几个属性:(暂时只测了IE和firefox,实际上我工作中用到的最多的是chrome)  网页被卷起来的高度/宽度(即浏览器滚动条滚动后隐藏的页面内容高度) (javascript)       ...

  10. Linux下追踪函数调用,打印栈帧

    事情的起因是这样的,之前同事的代码有一个内存池出现了没有回收的情况.也就是是Pop出来的对象没有Push回去,情况很难复现,所以在Pop里的打印日志,跟踪是谁调用了它,我想在GDB调试里可以追踪调用的 ...