洛谷 CF804B Minimum number of steps

Ok...

 

Topic link: https: //www.luogu.org/problemnew/show/CF804B

 

This question is no skills, just find a question of law.

First, see "ab" can be replaced with "bba", so we have to first determine reverse enumerate (Note s from s_0 start), if you encounter a, put ans + = cntb, because of the number of times you can change that is a b back number, then the number of by-two cntb, ab can change as a BBA, also a two transducer b b. If you encounter b, directly cntb ++, do not need another operation.

 

Note: mod can die at any time, it will not affect the final result.

 

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 const int mod = 1e9 + 7;
 8 char s[1000005];
 9 long long ans, cntb;
10 
11 int main(){
12     scanf("%s", s);
13     int len = strlen(s);
14     for(int i = len - 1; i >= 0; i--){
15         if(s[i] == 'a'){
16             (ans += cntb) %= mod;
17             (cntb *= 2) %= mod;
18         }
19         else cntb++;
20     }
21     printf("%lld\n", ans % mod);
22     return 0;
23 }
AC Code

 

Guess you like

Origin www.cnblogs.com/New-ljx/p/11234330.html