We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.
The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.
Input
The first line contains the initial string consisting of letters 'a' and 'b' only with length 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".
我想的思路没他人的巧妙,我就是直接找ab串然后分析4种情况。(这样找比较麻烦,换个角度有时会轻松许多)
1、ab前无a,ab后无b。
2、ab串前有a,ab后无b。
3、ab串前无a,ab后有b。
4、ab前有a,后有b。
接下来就是按题意模拟。具体看代码吧。
AC Code
#include<iostream> #include<iomanip> //#include<bits/stdc++.h> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #include<cstring> #include<vector> #include<sstream> #include<queue> #include<set> #define PI 3.14159265358979 #define LL long long const int INF=0x3f3f3f3f; const int MAX=1e6+10; const LL temp=1e9+7; using namespace std; char c[MAX]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>(c+1); LL l=strlen(c+1); LL sum=0; for(int i=1;i<=l;++i) { if(c[i]=='a'&&c[i+1]=='b') { if(c[i-1]!='a'&&c[i+2]!='b') { sum=(sum+1)%temp; } else if(c[i-1]!='a'&&c[i+2]=='b') { sum=(sum+1)%temp;LL o=1; LL k=i+2; while(c[k]=='b'&&k<=l) { sum=(sum+1)%temp; k++; } i=k-1; } else if(c[i-1]=='a'&&c[i+2]!='b') { LL k=i-1;sum=(sum+1)%temp;LL o=1; while(c[k]=='a'&&k>=1) { k--; o*=2; sum=(sum+o)%temp; } } else { sum=(sum+1)%temp;LL G=2,D=1; LL k=i+2,p=i-1;LL f=0; while(c[k]=='b'&&k<=l) { G+=2; D*=2; sum=(sum+D)%temp; k++; } i=k-1; while(c[p]=='a'&&p>=1) { sum=(sum+G)%temp; G*=2; p--; } sum--; } } } cout<<sum%temp; }