title: Codeforces-1084C

date: 2018-12-13 16:02:04

tags:

  • acm
  • 刷题

    categories:
  • Codeforces

概述

好久没写博客了,,,最近的事太多了,,几乎没有专门看一个知识点,,,一直在做各种的简单题QAQ。。。

题意

这道题大概的题意就是给你一个串,,,然后找出所有开头结尾都是a的并且中间有b的子串的个数,,,单一的a也算,,,

一开始看了好几遍都没看懂题意,,,不知道在求啥,,,

然后看了一个人的题解,,然后没看懂QAQ,,,,,

看了官方题解下的一个评论看懂了,,,,,emmmm

It doesn't depend at all if there exists any letter other than a or b in the given string. You can for sure ignore those letters, so the editorial says to erase them. Now, what you have is a string consisting only of a and b's. Also two consecutive b's can be merged as one. So your final string will look something like (a...a)b(a...a)b(a...)...

You can now consider this problem as sum of all possible product of subsets of a given set, where each element in the set is the number of a's delimited by b.

For example: In the string "aaabaabaaab", set formed will be {3,2,3,0} (0 can be ignored). Now if you have a set {a1,a2,...,aN}, then sum of all possible products of this set is equal to (1+a1)(1+a2)...*(1+aN)-1.

Proof:

Write the required answer as follows:

S = Sum of products of subset with (size=1)+(size=2)+...(size=N)

\(S = (a_1+a_2+...a_N)+(a_1*a_2+a_1*a_3......+a_{N-1}*a_N)+...+(a_1*a_2.....a_N)\)

After factorization,

S = (1+a1)(1+a2)...(1+aN)-1

大致意思就是处理所给的字符串,,,就变成了一堆a一个b一堆a一个b....这样的,,,

也就是一堆a的集合

题目所要的就是调两个个a的集合里调一个a作为子串的首尾,,,这样的就是所要的串,,,

于是总共的个数就是\(a_1*a_2*a_3.....a_{n-1}*a_{n}\),,,对了每个集合还要加一,,表示这个集合选一个或者都不选,,,最后的答案再减一就行了(全不选的情况不符合题意),,,

他上面那段话的思路是在计算所有的子川的情况时,,长度为1+长度为2+。。。长度为m。。。

这样的话 \(sum = (a_1 + a_2+....+a_{n-1} + a_n) + (a_1 * a_2 + a_1 * a_3 + ...+ a_1 * a_m + a_2 * a_3 + ......+ a_{n-1} * a_n) + (a_1 * a_2 * a_3 + ....) + ...\)

然后这个求和可以转化成\(sum = (1 + a_1) * (a_2 + 1) * (a_3 + 1) + (a_4 + 1) * .... * (a_n + 1) - 1\)

因为:

\(a + b + ab = (a + 1) * (b + 1) - 1\)

\(a + b + c + ab + ac + bc + abc = (a + 1) * (b + 1) * (c + 1) - 1\)

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7; ll a[maxn];
int main()
{
// freopen("233.txt" , "r" , stdin);
// freopen("233.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n = 0;
string s;cin >> s;
int a[maxn];
int tot = 0;
int len = s.length();
for(int i = 0; i < len; ++i)
{
if(s[i] == 'a')
{
int cnt = 0;
for(int j = i; j < len; ++j)
{
if(s[j] == 'a')
++cnt;
if(s[j] == 'b' || j == len - 1)
{
a[tot++] = cnt + 1;
i = j;
break;
} }
}
}
ll ans = 1;
for(int i = 0; i < tot; ++i)
ans = (ans * a[i]) % mod;
--ans;
cout << ans << endl;
return 0;
}

太水了,,,,QAQ

Codeforces-1084C的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

  10. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

随机推荐

  1. 分布式系统登录功能拦截器的实现以及cookie的共享问题(利用cookie实现session在分布式系统的共享)

    当我们的网站采用分布式部署系统时,每个子系统拥有自己独立的session,如果不实现session共享,当用户切换系统访问的时候,会不停的提示登录,这对于用户体验是非常不好的.因此对于多个子系统的的访 ...

  2. k8s如何管理Pod(rc、rs、deployment)

    是豆荚,可以把容器想像成豆荚里的豆子,把一个或多个关系紧密的豆子包在一起就是豆荚(一个Pod).在k8s中我们不会直接操作容器,而是把容器包装成Pod再进行管理(关于Pod,大家可以参考第十期的分享“ ...

  3. AngularJs分层结构小demo

    后端mvc分层,前端也要分层才对嘛.分层的好处不言而喻.简直太清晰,容易维护.反正清爽的一逼.不信你看. 思路:分为controller层和service层.controller层再提取一个公共的层. ...

  4. bzoj千题计划256:bzoj2194: 快速傅立叶之二

    http://www.lydsy.com/JudgeOnline/problem.php?id=2194 相乘两项的下标 的 差相同 那么把某一个反过来就是卷积形式 fft优化 #include< ...

  5. CS53 C 单调栈

    给出一个目标序列,初始序列为0,你有一种操作方式可以将某段值相同的区间全部加上一定的值,问得到目标序列的最小次数. 开始没注意要求值相同,想都不想就暴力了,后来发现对于每个峰,只要找每个相对峰顶的阶数 ...

  6. 官方资料&一些好的博客与技术点

    https://technet.microsoft.com/zh-cn/library/hh848794.aspxzh   https://msdn.microsoft.com/en-us/power ...

  7. Mac安装WineHQ

    下载: (链接: https://pan.baidu.com/s/1o7NPhNk 密码: 5227) 安装: 先决条件: XQuartz>=2.7.7 系统设置允许未签名的包. 在https: ...

  8. 为ASP.NET控件加入快捷菜单

    ContextMenu Control 快捷菜单控件概述: MSDN Liabrary 中包含了几个DHTML快捷菜单的示例.分别提供了对这一功能的不能实现方法.一个快捷菜单就是在页面中任何位置的一组 ...

  9. CSS-3 box-shadow 的使用

    box-shadow是给对象实现图层阴影效果的. 语法: E {box-shadow: <length> <length> <length>?<length& ...

  10. Dream------Hadoop--网络拓扑与Hadoop--摘抄

    两个节点在一个本地网络中被称为“彼此的近邻”是什么意思?在高容量数据处理中,限制因素是我们在节点间 传送数据的速率-----带宽很稀缺.这个想法便是将两个节点间的带宽作为距离的衡量标准.   衡量节点 ...