Count Good Substrings
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的更多相关文章
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 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 ...
- 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 ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [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 ...
随机推荐
- hdu2030java
汉字统计 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...
- Android开发之Activity(活动)
在安卓中,Activity(活动)就是一个包含应用程序的用户界面的窗口.一个应用程序可以包含一个或多个Activity. 一般一个活动对应一个UI文件,即xml文件.创建活动一般是基础Activity ...
- GetTickCount() 函数的作用和用法
今天项目中60秒倒计时模块需要用到GetTickCount(),这个函数,在此做下整理和总结. 1.定义 For Release configurations, this function retur ...
- Plain old data structure(POD)
Plain old data structure, 缩写为POD, 是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中.POD通常被用在系统的边界处,即指不同系统之间只 ...
- 推断类型var
1.为什么使用推断类型var var可以根据变量的初始值自动推断局部变量类型,当无法确定所用变量的具体类型时可使用var 2.如何使用推断类型var 客户端代码 static void Main(st ...
- 用Javascript评估用户输入密码的强度
<!-- 密码已经是我们生活工作中必不可少的工具,但一个不安全的密码有又有可能会给我们造成不必要的损失.作为网站设计者,如果我们在网页中能对用户输入的密码进行安全评估,并显示出相应的提示信息 ...
- 样式优先级、margin
margin:上 左 下 右:
- asp.net <%%> <%#%><%=%><%@%><%$%>用法与区别
1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...
- 消除热块(hot block)
上篇日志提到了,那么高的负载,是存在数据块读竞争,下面介绍几个方法来消除块竟争 查找块竟争 SELECT p1 "file#", p2 "block#", p3 ...
- Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)
一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...