time limit per test
1 second
memory limit per test
256 megabytes
We have a string of letters 'a' and 'b'. We want toperform some operations on it. On each step we choose one of substrings "ab" in thestring and replace it with the string "bba". If wehave no "ab" as a substring, our job is done. Print the minimumnumber of steps we should perform to make our job done modulo 109 + 7.
The string "ab" appearsas a substring if there is a letter 'b' right after the letter 'a' somewhere inthe string.
Input
The first line contains the initialstring consisting of letters 'a' and 'b' only withlength from 1 to 106.
Output
Print the minimum number of steps modulo 109 + 7.
Examples
Input
ab
Output
1
Input
aab
Output
3
Note
The first example: "ab" → "bba".
The second example: "aab" → "abba" → "bbaba" → "bbbbaa".
#include <bits/stdc++.h> using namespace std; #define mst(a,b) memset((a),(b),sizeof(a)) #define f(i,a,b) for(int i=(a);i<=(b);++i) #define ll long long const int maxn = 1e6+5; const ll mod = 1e9+7; const int INF = 0x3f3f3f3f; const double eps = 1e-6; #define rush() int T;scanf("%d",&T);while(T--) char s[maxn]; int main() { ll cnt,ans; while(~scanf("%s",s)) { int len=strlen(s); cnt=ans=0; for(int i=len-1;i>=0;i--) { if(s[i]=='b') { cnt++; } else if(s[i]=='a') { ans+=cnt; ans%=mod; cnt*=2; cnt%=mod; } } printf("%I64d\n",ans); } return 0; }