您的位置:首页 > 其它

B. Minimum number of steps

2017-09-28 21:29 537 查看
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".

解题说明:此题是一道字符串题,可以这样想,如果题目要求是把“ab”转换成“ba”的话,就相当于把所有的b移到前面去,把a移到后面去。但是现在在转换的过程中增添了一个‘b’,因此在逆序的时候,每次遇到一个“b”就把累积的的变量加1,如果遇到”a”,就把累积变量累加的到总和里,并且把累积变量乘以2,因为每次转换都会把a移动到后面,而前面留下了两个b,对前面的a造成影响。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;

#define MOD 1000000007
int main()
{
char a[1000005];
scanf("%s", a);
int i;
long long ans = 0, b = 0;
for (i = strlen(a) - 1; i >= 0; i--)
{
if (a[i] == 'b')
{
b++;
}
else if (a[i] == 'a')
{
ans += b;
b <<= 1;
}
ans %= MOD;
b %= MOD;
}
printf("%lld\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: