Codeforces Round #411 (Div. 2)D 规律题

xiaoxiao2021-02-27  228

题目链接

通过观察,易发现 ab --> bba 是将a,b换一个位置,然后在中间再加一个b。

那么我们通过模拟和总结,可以得出以下两个结论:

形如 abbb,因为变换的本质是交换顺序,那么如果不存在ab,则说明a在b的右边,故需要将a和其右边的所有b交换顺序,即ans = 3。

形如 abbb,因为每次变换都会在a,b交换顺序后中间插入一个b,故当a移动到最右边时,b的数量会变成之前的两倍。

结合以上两点,我们可以从后往前遍历字符串,动态维护b的数量cnt,若:

当前元素是b : cnt++当前元素是a:更新答案,并更新b的数量(乘2)

以上。 代码:

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int A = 1e6 + 100; const int mod = 1e9 + 7; char s[A]; int main(){ scanf("%s",s); ll cnt = 0; ll ans = 0; int len = strlen(s); for(int i=len-1 ;i>=0 ;i--){ if(s[i] == 'b') cnt++; else{ ans = (ans + cnt) % mod; cnt = cnt * 2 % mod; } } printf("%I64d\n",ans); return 0; }
转载请注明原文地址: https://www.6miu.com/read-11872.html

最新回复(0)