Codeforces 804B

来源:互联网 发布:网络防攻击软件 编辑:程序博客网 时间:2024/06/05 21:14
B. Minimum number of steps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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".

思路:推算几个样例之后会发现,最后所有的串都会变成bbb...bbaaaaa...aa形式,这样就可以逆序处理这个串,当这个位置为a时,答案就是b个数,当然此时替换之后b个数会变为原来的2倍,否则b个数加加。当然题目要求模上一个数,在中间处理过程中需要注意模上。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
int main()
{
    string s;
    while(cin>>s)
    {
        int len=s.length();
        LL ans=0,num_b=0;
        for(int i=len-1;i>=0;i--)
        {
            if(s[i]=='a')
            {
                ans=(ans+num_b)%mod;
                num_b=(2*num_b)%mod;
            }
            else num_b++;
        }
        cout<<ans%mod<<endl;
    }
    return 0;
}