codeforces 451D Count Good Substrings
题意:给定一个字符串,求有多少个奇数子串和多少偶数子串为 “回文串” 这边回文串很特殊之含有 ab 两种字母 而且 相邻的字母相同则消去一个 一直到不存在相邻的相同。
思路: 在这种串中 ,消到最后 一定是 abababababa。。。 或者 bababababab。。。 那么 只要头尾一样 那么这个串 一定是 回文串。
那么 只需要 统计下 奇数位上 和 偶数位上a b个数就能直接计算。 一个在奇数位一个在偶数为 长度位偶数, 两个都在 奇数位 或者偶数位 则长度为奇数。
#include <iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define LL long long
#define f(x) ((x)*(x+1)/2)
using namespace std;
char s[100050];
int main() {
LL odda,oddb,evena,evenb;
odda=oddb=evena=evenb=0;
scanf(" %s",s);
int len=strlen(s);
for(int i=0;i<len;++i)
{
if(s[i]=='a')
{
if(i&1)
odda++;
else
evena++;
}
else
{
if(i&1)
oddb++;
else
evenb++;
} }
LL ans1=odda*evena+oddb*evenb;
LL ans2=f(odda)+f(oddb)+f(evena)+f(evenb);
printf("%I64d %I64d\n",ans1,ans2);
return 0;
}
codeforces 451D Count Good Substrings的更多相关文章
- codeforces D. Count Good Substrings
http://codeforces.com/contest/451/problem/D 题意:给你一个字符串,然后找出奇数和偶数长度的子串的个数,这些字串符合,通过一些连续相同的字符合并后是回文串. ...
- 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 ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- Codeforces 451D
题目链接 D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- Codeforces Round #258 D Count Good Substrings --计数
题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式 ...
- 【Codeforces 258D】 Count Good Substrings
[题目链接] http://codeforces.com/contest/451/problem/D [算法] 合并后的字符串一定是形如"ababa","babab&qu ...
随机推荐
- VS2010使用TTS
最近在写纯车牌停车管理系统,需要用到语音合成功能. 目前最常用的Windows Speech SDK版本有三种:5.1.5.3和5.4. Windows Speech SDK 5.1版本 ...
- URL编码与解码
在项目中碰到了ajax传来的参数,后台接收值乱码(如下图)的问题 在此记录一下 前台: 后台: 解决问题 为什么需要编码 怎样编码 实际出现的问题解决方法 1.为什么需要编码? URL 只能使用 AS ...
- How to locate a path? - Unix & Linux Stack Exchange
How to locate a path? - Unix & Linux Stack Exchange http://unix.stackexchange.com/questions/2955 ...
- Linux应用程序访问字符设备驱动详细过程【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51346532 下面先通过一个编写好的内核驱动模块来体验以下字符设备驱动 可以暂时 ...
- tomcat普通用户运行
网站绑定域名后直接通过域名访问使用的是80端口,因此tomcat须监听80端口,而为了安全起见tomcat一般不用root身份运行,综上,需要以普通用户来运行监听80端口的tomcat.此时就会启动失 ...
- noi 4977 怪盗基德的滑翔翼
题目链接: http://noi.openjudge.cn/ch0206/4977/ LIS http://paste.ubuntu.com/23406594/
- html5 svg动画
http://www.zhangxinxu.com/sp/svg/ 以上是svg的一个线上编辑器,也可以adobe Illustrator制作生成. 我们通过以上编辑器可以获得以下代码. 例: < ...
- fidder 使用教程
fidder 使用教程 1. Fiddler 是什么? Fiddler是用C#编写的一个免费的HTTP/HTTPS网络调试器.英语中Fiddler是小提琴的意思,Fiddler Web Debugge ...
- Bootstrap_按钮工具栏
单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等. Bootstrap框架为大家提供了按钮组组件. <div ...
- php中session机制的详解
[补充]session_start()要放在php最前面,header()函数也要放在session_start()之后. [读了下面的文章转载的文章后自己的理解]: 1,通过phpinfo()函数可 ...