• CF-805D


    D. Minimum number of steps
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Input

    The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.

    Output

    Print the minimum number of steps modulo 109 + 7.

    Examples
    input
    ab
    output
    1
    input
    aab
    output
    3
    Note

    The first example: "ab"  →  "bba".

    The second example: "aab"  →  "abba"  →  "bbaba"  →  "bbbbaa".

    题意:

    对于给定字符串,我们可将其中的子串“ab”替换成“bba”,直到没有子串“ab”。

    求操作次数。

    由题意可知,a后的一个b会变成a前的两个b。

    我们可以反向遍历,记录b的个数,每遇到a做一次操作。b*=2.继续。

    附AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int mod=1e9+7;
     5 
     6 int main(){
     7     string s;
     8     cin>>s;
     9     long long ans=0,cnt=0;
    10     int n=s.size();
    11     for(int i=n-1;i>=0;i--){
    12         if(s[i]=='b')
    13         cnt++;
    14         else{
    15             ans+=cnt;
    16             ans%=mod;
    17             cnt=cnt*2%mod;
    18         }
    19     }
    20     cout<<ans%mod<<endl;
    21     return 0;
    22 }
  • 相关阅读:
    javaday19_List接口_Set接口
    01玩转数据结构_04_最基础的动态数据结构:链表
    10 拖拽的对话框_滚动条_放大镜_
    01玩转数据结构_03_栈和队列
    java小技巧
    01玩转数据结构_02_不要小瞧数组
    01玩转数据结构_01_课程介绍
    javaday18_ArrayList
    JZOJ.3777【NOI2015模拟8.17】最短路(shortest)
    JZOJ.5230【NOIP2017模拟8.5】队伍统计
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6815063.html
Copyright © 2020-2023  润新知