题目传送门
题解
题目理解与强调:
这道题目含有一条只包含小写字母的字符串。要求能够找到多少个 子序列 满足形式 $ a,b,\ldots,a,b $
思路分析:
这道题是一道计数题。
很简单地,我们可以得出结论:题目的答案与字符串$ s $中的’$ a,b,\ldots,a,b $’的数量与位置有关,当我们读取到’$ a $’时,首先要在答案的基础上$ +1 $ ,因为我们可以让它单独成为一个形如 ‘$ a $’的子序列。同时可以构成$ sum $个形如’$ a,b,\ldots,a,b $’的序列,$ sum $ 就是距离这个’$ a $’最近的’$ b $’之前所有满足形式的子序列个数,因为我们可以通过这个’$ b $’ 与之前的所有子序列合起来构成一个新的子序列。所以,在读取字符是读到’$ b $’时,将一个变量$ p $赋值为$ sum $,当读取到’$ a $’时,将$ sum $ 加上 $ p+1 $,最后输出结果。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
#define pr(x) cerr<<#x<<"="<<(x)<<endl
#define pri(x,lo) {cerr<<#x<<"={";for (int ol=0;ol<=lo;ol++)cerr<<x[ol]<<",";cerr<<"}"<<endl;}
#define inf 100000000
#define N 1000
template<class T>inline void read(T &x)
{
x=0;register char c=getchar();register bool f=0;
while(!isdigit(c))f^=c=='-',c=getchar();
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(f)x=-x;
}
template<class T>inline void print(T x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar('0'+x%10);
}
int main()
{
register char c;
register int sum=0,p=0;
while(c=getchar())
{
if(c<'a'||c>'z') break;
if(c=='a') sum=(sum+p+1)%1000000007;
if(c=='b') p=sum;
}
print(sum);
return 0;
}