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 ...
随机推荐
- 自主创建tcpdump/wireshark pcap文件
pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump.wireshark等等,了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试. p ...
- xshell十大技巧
xshell是我用过的最好用的ssh客户端工具,没有之一.这个软件完全免费,简单易用,可以满足通过ssh管理linux vps所有需要,唯一遗憾的是没有官方中文版. 警告:不要下载所谓的汉化版,可能有 ...
- cookie 和 HttpSession
保存会话数据的两种技术 Cookie Cookie 是客户端技术,程序把每个用户的数据以cookie的形式写给用户的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.web ...
- linux下的openoffice安装和服务自启动
openoffice下载并安装 wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/zh-CN ...
- C#入门教程(二)–C#常用快捷键、变量、类型转换-打造C#学习教程
C#入门教程(一)–.Net平台技术介绍.C#语言及开发工具介绍-打造C#学习教程 上次教程主要介绍了.Net平台以及C#语言的相关介绍.以及经典程序案例,helloworld程序. 初来乍到,第一次 ...
- javascript 定时器使用
定时器的使用场合 setInterval() 方法,按照指定的周期(以毫秒记)来调用函数或计算表达式setInterval() 方法会不停的调用函数,知道 clearInterval() 被调用或窗口 ...
- web应用程序servlet的映射名称的规则及请求过程
首先用MyEclipse创建一个web Project(工程名起为TestServletProject),新建一个Servlet(这里servlet的名字起TestServlet),将请求的servl ...
- Java之字符串学习
java中String的使用十分频繁,是我们要学习的重点,在说String之前,我们要知道堆跟栈的区别. java中的数据类型分原生数据类型(primitived types)有八种(byte,cha ...
- 分享一个自己写的基于TP的关系模型(2)
1.增加多对多关系的处理 /** * 定义关系 * @return array */ public function test4(){ //参数说明 //关联的模型 //主表关联字段 //关联中间表 ...
- android入门到熟练(一)
1.andro系统架构:Linux内核层(提供驱动),系统运行库层和android运行时库(提供C/C++库的主要特性,如SQLite,OpenGL,Webkit等和Dalvik虚拟机),应用框架层, ...