CodeForces - 805D

D. Minimum number of steps
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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".
Print the minimum number of steps modulo 109?+?7.
题意:给你一个只含'a'和'b'的串,让你把所有"ab"变为"bba",直到串中不含"ab"的子串,输出共变了多少次;
思路:仔细看提示的样例,可以发现最后的串中'a'的数量不变,那是不是可以看作把当前串的最后一个'a'移动到当前串中最后一个'b'后面,让ans加上移动的次数,并且'a'每移动一次,'b'的个数加一,从尾到头跑一遍,输出ans即可;
#include<iostream>
#include<string.h>
using namespace std;
const int mod = 1e9 + 7;
int main()
{
 long long cnt = 0;
 long long ans = 0;
 char a[1000005];
 scanf("%s", a);
 int len = strlen(a);
 for (int i = len-1; i>=0; i--)
  if (a[i] == 'b')
   cnt++;
  else
  {
   ans=(ans+cnt)%mod;
   cnt= (cnt+(cnt%mod))%mod;
  }
 cout << ans << endl;
 return 0;
}

posted @ 2017-05-23 22:22  DNoSay  阅读(110)  评论(0编辑  收藏  举报