codeforce 804B Minimum number of steps

cf劲啊

原题:

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 10^9?+?7.

The string "ab" appears as a substring if there is a letter ‘b‘ right after the letter ‘a‘ somewhere in the string.

the initial string consisting of letters ‘a‘ and ‘b‘ only with length from 1 to 10^6.

大意:

给一个ab串,你每次能挑一个ab变成bba,问至少变多少次不能再变

串长1e6,答案膜1e9+7

b……bba?

我就是叫紫妈怎么了?

恩核心思路就是ab变成bba后相当于把a往右挪了一下并在a前面添加一个b

这样就好写了从右往左扫,如果遇到b就把b的个数+1,如果遇到a就把答案加上a后面b的个数并把b的个数翻倍

因为是从右往左扫的,这样就保证a往右挪的时候为后面的a添加的b的个数尽可能少,保证答案最小

不需要考虑一串a连在一起的情况,如果最右边全是a,那么不用管了对答案没贡献,就算左边的a挪过去也越不过这一串a

如果中间是一串a,那么右端点一定接了一个b,最右边的a挪过去后次右边又接上了b,这样就能保证所有的a都能挪到末尾

代码很好写,python13行(没压行,写的比较散

代码:

 1 mo = 1000000007
 2 a = raw_input()
 3 b, c = 0, 0
 4 i = len(a) - 1
 5 while i >= 0:
 6     if a[i] == ‘a‘:
 7         c = (c + b) % mo
 8         b = (b * 2) % mo
 9     else:
10         b = (b + 1) % mo
11     i = i - 1
12
13 print c

时间: 2024-10-03 23:10:06

codeforce 804B Minimum number of steps的相关文章

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description 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 subs

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram

题目 用hash,比较两个字符串数组的每个字符的hash值 class Solution { public: int m[300]; int m2[300]; int minSteps(string s, string t) { for(int i=0;i<s.length();i++) { m[s[i]]++; } for(int i=0;i<t.length();i++) { m2[t[i]]++; } int ans=0; int steps=0; for(int i=0;i<30

CF804B Minimum number of steps

思路: 找规律.参考了http://blog.csdn.net/harlow_cheng/article/details/71190999. 实现: 1 #include <iostream> 2 using namespace std; 3 const int mod = 1e9 + 7; 4 int main() 5 { 6 string s; 7 cin >> s; 8 int n = s.length(); 9 int cnt = 1; 10 int sum = 0; 11

Codeforces 805 D Minimum number of steps

题意: 给定一串字符串,将所有"ab"的子串替换为"bba",询问多少次操作后没有子串"ab". 分析: 观察可得,将"ab"替换为"bba"有两种结果. ①a移到了b的后面 ②增加了一个b 而且最终的结果一定是前面全是b,后面全是a. 所以可以猜想从后往前数,设置一个B_cnt, 每当碰到一个b, 就b_cnt++, 碰到A, 就先加上一个b_cnt(因为每替换一次会将a移动后一格,所以要替换b_cnt次

【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge. Return the min

Minimum number of swaps required to sort an array

https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unordered array consisting of consecutive integers  [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find t

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Interval Minimum Number

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the resu