https://codeforces.com/problemset/problem/804/B
翻译
我们有一串字母'a'和'b'。我们想对它进行一些操作。在每一步中,我们选择字符串中的一个子字符串“ab”,并将其替换为字符串“bba”。如果我们没有“ab”作为子字符串,我们的工作就完成了。打印按109 + 7取模完成任务所需的最小步骤数。
如果字符串中某个位置的字母“a”后面紧接字母“b”,则字符串“ab”将显示为子字符串。
输入
第一行包含由字母'a'和'b'组成的初始字符串,长度从1到10^6。
输出
输出按109 + 7取模的最小步数。
例子
inputCopy
ab
outputCopy
1
inputCopy
aab
outputCopy
3.
请注意
第一个例子:"ab"→"bba"。
第二个例子:"aab"→"abba"→" baba"→"bbbbaa"。
思路
// 多拿几个串试一遍就知道,最后状态必然形如:bbb...bbaa...a
// 我们逆序处理该字符串,如果遇到一个'a',那么就看一下它之前有几个'b',答案就加上几个,同时'b'的数量要翻倍
// 如果遇到一个'b',那么'b'的数量就加一,同时答案保持不变
// 因为与'a'直接相邻的'b'的数量则是要被替换成"bba"的次数,然后每次做完这些替换之后'b'的个数会翻倍
// 最后,要注意取模
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define pi acos(-1.0)
#define endl '\n'
#define x first
#define y second
typedef long long LL;
priority_queue<int> pq;
priority_queue<int, vector<int>, greater<int> > up;
deque<int> dq;
typedef pair<LL, LL> PII;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10, mod = 1e9 + 7;
int f[N];
void solve()
{
string s;
cin >> s;
int b = 1,num = 0;
for(int i = 0; i < s.size(); i ++)
{
if(s[i] == 'a')
{
b = b * 2 % mod;
}
else
{
num += b - 1;
num %= mod;
}
}
cout << num << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(0);
int T = 1;
//cin >> T;
while(T --)
{
solve();
}
return 0;
}