1. 程式人生 > >codeforce 804B Minimum number of steps

codeforce 804B Minimum number of steps

給一個 只包含 ‘a’,’b’的string 。現有 opeartion 將 string 中的 substring 中的“ab”,替換成“bba”。求 將所有 子串替換換完的最小操作次數。

ab->bba;
aab-> a bba-> bba ba-> bb bbaa
aaab-> a abba->a bbaba->a bbbbaa-> bb a bbba-> bb bba bbaa
->bbbb bba baa -> bbbb bb bba aa 。
從中可以發現:就是把 前面 所有 a 移到 該個 b 之後,如果前面有 n個a,就要移動(1+2+4+……+2^(n-1) )次。

#include <bits\stdc++.h>
using namespace std;
const int maxn = 1e5+50;

#define FCIN  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

const int MAXINT = 0x7fffffff;
const int MININT = 0x80000000;
const int mod = 1e9+7; 

typedef long long LL;

char s[maxn*10]; 

int my_pow(int a,int b){
    LL ans  = 1
, q = a; while(b){ if(b&1){ ans *= q; ans%=mod; } q*=q; q%=mod; b>>=1; } return ans; } int main(){ scanf("%s",s); long long ans = 0; int len =strlen(s); int numa = 0, numb = 0; for(int i=0;i<len;i++){ if
(s[i]=='a') numa+=1; if(s[i]=='b'){ ans += my_pow(2,numa)-1; ans %= mod; } } printf("%lld\n",ans); return 0; }

相關推薦

codeforce 804B Minimum number of steps

bst 代碼 img 添加 close perf rep spl pri cf勁啊 原題: We have a string of letters ‘a‘ and ‘b‘. We want to perform some operations on it. On each

codeforce 804B Minimum number of steps

給一個 只包含 ‘a’,’b’的string 。現有 opeartion 將 string 中的 substring 中的“ab”,替換成“bba”。求 將所有 子串替換換完的最小操作次數。 ab-&

CF804B Minimum number of steps

ron urn names har ber int cin con href 思路: 找規律。參考了http://blog.csdn.net/harlow_cheng/article/details/71190999。 實現: 1 #include <iostre

CodeForces - 805D Minimum number of steps

mini png turn 增加 include blog 裏的 nbsp style 題目大意:把一個串裏的ab改為bba,求最少改幾次就沒有ab了 具體思路:可以把一次操作看成把a放到b右邊,再增加一個b,要求把所有的a都移到b右邊 發現1個a過一個b要1次操作 2

[Coding Made Simple] Coin Changes Minimum Number of Coins

sim == pri chan set ble length nco one Given coins of certain denominations and a total, how many minimum coins would you need to make th

452. Minimum Number of Arrows to Burst Balloons紮氣球的個數最少

-c 分析 limit 處理 排序 什麽 exactly 循環 ase [抄題]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pro

Minimum number of swaps required to sort an array

-h continue str num dice fill cond main lse https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II

Leetcode 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

LeetCode 452. Minimum Number of Arrows to Burst Balloons & 435. Non-overlapping Intervals

題解 兩題一起寫,都是pair型陣列,都考慮交疊問題,都是貪心。 這類題的核心都在於預先sort一下,以second升序排(若相等再以first升序)。 這樣的好處是,我們再順序遍歷的時候可以很方便地發現交疊情況: 比如用一個 p 變數記載之前的second,新位置比較一下first

Greedy--Minimum Number of Arrows to Burst Balloons (Medium)

原題: Problem Description There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided inp

[leetcode]452. Minimum Number of Arrows to Burst Balloons

[leetcode]452. Minimum Number of Arrows to Burst Balloons Analysis 好冷鴨—— [每天刷題並不難0.0] There are a number of spherical balloons spr

Google - Find minimum number of coins that make a given value

Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum

leetcode 452 Minimum Number of Arrows to Burst Balloons(射擊氣球最小射擊手數目)貪心

題目 思路: 活動選擇問題,會議室選擇問題開展最多會議數目,其實一一個型別 將開始時間(座標)和結束時間排序(任選一個就行,我看c++做法裡面有按照開始時間排序,本體中是結束時間排序,感覺按照結束時間排序更合理) 將所有的氣球按照終止位置排序,開始從前向後掃描。以第一個氣球

python leetcode 452. Minimum Number of Arrows to Burst Balloons

能重疊的區域能一起射爆 所以先排序 再取重合的區域 看一共有幾個這樣的區域 class Solution: def findMinArrowShots(self, points): """ :type points: List[List[int]]

LeetCode 題解: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 coo

LeetCode 452. Minimum Number of Arrows to Burst Balloons & 435. Non-overlapping Intervals

題解 兩題一起寫,都是pair型陣列,都考慮交疊問題,都是貪心。 這類題的核心都在於預先sort一下,以second升序排(若相等再以first升序)。 這樣的好處是,我們再順序遍歷的時候可以很方便地發

LC 871. Minimum Number of Refueling Stops

posit 是什麽 least ini 什麽 sid cannot for when A car travels from a starting position to a destination which is target miles east of the star

[LeetCode] 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 horizo

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 coordina

The minimum number of elements to reach the end of an array

Given an array of integers, each element represents the max number of jumps that you can move forward. Write a piece of code to find out