CodeForces 805D Minimum number of steps
规律。
手动模拟几组数据,可以发现规律,总的来说就是把$b$和$a$分离,$b$在前面,$a$在后面,且每次将一个$a$往后调整$1$个位置,$b$会多一个,那么从后往前算一遍就可以得到答案了。
#include <cstdio>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
char s[1000010];
long long mod = 1e9+7;
int main()
{
scanf("%s",s);
int len = strlen(s);
long long ans = 0;
long long L = 0;
long long A = 0;
for(int i=len-1;i>=0;i--)
{
if(s[i]=='a')
{
ans = (ans+L-A+mod)%mod;
L = (L+L-A+mod)%mod;
L++;
A++;
}
else L++;
}
printf("%lld\n",ans);
return 0;
}

浙公网安备 33010602011771号