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.
The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.
Print the minimum number of steps modulo 109 + 7.
ab
1
aab
3
The first example: "ab" → "bba".
The second example: "aab" → "abba" → "bbaba" → "bbbbaa".
思路+规律~
似乎就是找规律啊,不知道该怎么归类呢。
设第i位右面的b的个数为a[i],则
设最靠右的在b左面的a为k,在这个a移到所有b右面之后,它左面所有a的a[i]值都要加上a[k],所以我们应该先移右面的,再移左面的。移动前面所有点对于当前点的代价相当于是前缀和*2再加上当前点a[i],所以只要求前缀和就可以了。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define ll long long
#define modd 1000000007
ll n,now,num,ans,tot;
char s[1000001];
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=n;i;i--)
if(s[i]=='b') now++,now%=modd;
else num=(num*2+now)%modd,tot=(tot+num)%modd,now=0;
printf("%d\n",tot);
return 0;
}