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 ...
随机推荐
- 鸟哥的linux私房菜学习记录之系统设定工具与硬件检测
这部分没啥用,就不记录了,关于服务器的在服务器篇
- UBI系统原理-中【转】
转自:http://blog.chinaunix.net/uid-28236237-id-4217118.html UBI 文件头位置 EC头都放置在擦除块的开始偏移位置,占用64字节空间.之后防止V ...
- html+css创建提示框
看到下面的效果了吗? 本来我们站点是用下面的图片做的背景, 但是后期当更改完框中的提示内容,并且更新内容较多的时候,发现内容溢出了,如下图: 但是背景图片不能自动拉伸,还得重新做一张背景图,这样就导致 ...
- 一个用php写的人民币数字转人民币大写的函数
function num2rmb ($num) { $c1 = "零壹贰叁肆伍陆柒捌玖"; $c2 = "分角元拾佰仟万拾佰仟亿"; ...
- GRUB学习笔记(转自http://www.cnblogs.com/evilzy/archive/2008/03/30/1130173.html)
grub学习笔记1 首先要了解的几个概念 1.1 启动管理器 启动管理器是存储在磁盘开始扇区中的一段程序,例如,硬盘的MBR(Master Boot Record),在系统完成启动测试后,如果系统是从 ...
- tomcat域名访问配置
模拟线上环境,在本地以域名访问系统思路 1.首先在hosts文件将域名映射为本地IP 2.假如服务器80端口已被占用,可以用nginx转发,在nginx/vhosts/abc.com加入如下配置 se ...
- keytool的使用
1. 创建数字证书 keytool -genkey -v -alias scent -dname "CN=John,OU=MNG,O=Corp,L=Hangzhou,ST=Zhejiang, ...
- Android官方数据绑定框架DataBinding
数据绑定框架给我们带来了更大的方便性,以前我们可能需要在Activity里写很多的findViewById,烦人的代码也增加了我们代码的耦合性,现在我们马上就可以抛弃那么多的findViewById. ...
- 151102SQL语句
$condition = array($pk=>array('in',explode(',',$id)));//??? $this->model->where($condition) ...
- springMVC简单示例
1.新建web工程 2.引入springframework架包 3.配置文件 web.xml <?xml version="1.0" encoding="UTF-8 ...