zoukankan      html  css  js  c++  java
  • Codeforces 805D/804B

    传送门:http://codeforces.com/contest/805/problem/D

    对于一个由‘a’、‘b’组成的字符串,有如下操作:将字符串中的一个子串“ab”替换成“bba”。当字符串中不含有子串“ab”时,任务完成。求完成任务的最小操作次数(mod109+7)。

    最终,字符串的形式为:

    bbb...baaa...a

    可以考虑寻找规律:

    a.

    i)“ab”→“bba”,

    ii)“aab”→“abba”→“bbaba”→“bbbbaa”,

    iii)“aaab”→“aabba”→“abbaba”→“bbababa”→“bbbbaaba”→“bbbbabbaa”→“bbbbbbabaa”→“bbbbbbbbaaa”;

    b.

    i)“ab”→“bba”,

    ii)“abb”→“bbab”→“bbbba”,

    iii)“abbb”→“bbabb”→“bbbbab”→“bbbbbba”。

    可见,$"underbrace{acdots a}_{n}b"$型字符串的操作次数为2n-1,$"aunderbrace{bcdots b}_{m}"$型字符串的操作次数为m。下证之:

    a.(证:$"aunderbrace{bcdots b}_{m}"$型字符串的操作次数为m

    i)当m=1时,“ab”→“bba”,操作次数为1;

    ii)假设当m=k时,$"aunderbrace{bcdots b}_{k}"$→$"underbrace{bcdots b}_{2k}a"$,操作次数为k,则当m=k+1时:

    由$"aunderbrace{bcdots b}_{2k+1}b"$→$"underbrace{bcdots b}_{2k}ab"$的操作次数为k,由$"underbrace{bcdots b}_{2k}ab"$→$"underbrace{bcdots b}_{2k}bba"$的操作次数为1。故由$"aunderbrace{bcdots b}_{2k+1}"$→$"underbrace{bcdots b}_{2k+2}a"$的操作次数为k+1。

    b.(证:$"underbrace{acdots a}_{n}b"$型字符串的操作次数为2n-1)

    i)当n=1时,“ab”→“bba”,操作次数为1;

    ii)假设当n=k时,$"underbrace{acdots a}_{k}b"$→$"underbrace{bcdots b}_{2^{k}}underbrace{acdots a}_{k}"$,操作次数为2k-1,则当n=k+1时:

    由$"aunderbrace{acdots a}_{k}b"$→$"aunderbrace{bcdots b}_{2^{k}}underbrace{acdots a}_{k}"$的操作次数为2k-1,由$"aunderbrace{bcdots b}_{2^{k}}underbrace{acdots a}_{k}"$→$"underbrace{bcdots b}_{2^{k}}underbrace{bcdots b}_{2^{k}}aunderbrace{acdots a}_{n}"$的操作次数为2k。故由$"underbrace{acdots a}_{k+1}b"$→$"underbrace{bcdots b}_{2^{k+1}}underbrace{acdots a}_{k+1}"$的操作次数为2k-1+2k=2k+1-1。

    考虑一般的情形:

    设字符串的长度为len,其中,字符‘b’占据位置p1,p2,...,pn。这个字符串对应的操作次数为:$sum_{i=1}^{n}(2^{p_{i}-i}-1)$。

    若1..pi的字符‘a’个数为cnti,则cnti=pi-i。这个字符串对应的操作次数为:$sum_{i=1}^{n}(2^{cnt_{i}}-1)$。

    下证之:

    A为只含有字符‘a’的字符串,B为只含有字符‘b’的字符串;AB可为空。)

    i)当n=1时,$"underbrace{acdots a}_{cnt_{1}}underset{1}{b}"$→"BA",操作次数为$2^{cnt_{1}}-1$;

    ii)假设当n=k时,$"Aunderset{1}{b}cdots Aunderset{k}{b}cdots"$→"BA"的操作次数为$sum_{i=1}^{k}(2^{cnt_{i}}-1)$,则当n=k+1时:

    由$"Aunderset{1}{b}cdots Aunderset{k}{b}Aunderset{k+1}{b}cdots"$→$"BA*underset{k+1}{b}cdots"$的操作次数为$sum_{i=1}^{k}(2^{cnt_{i}}-1)$,A*的长度为cntk+1,则由$"BA*underset{k+1}{b}cdots"$→"BA"的操作次数为$2^{cnt_{k+1}}-1$。故由$"Aunderset{1}{b}cdots Aunderset{k}{b}Aunderset{k+1}{b}cdots"$→"BA"的操作次数为$sum_{i=1}^{k+1}(2^{cnt_{i}}-1)$。

    参考程序如下:

    #include <stdio.h>
    #define MOD 1000000007
    
    int pwr(long long x, int p)
    {
        if (p == 0) return 1;
        if (p & 1) return x * pwr(x, p - 1) % MOD;
        return pwr(x * x % MOD, p >> 1) % MOD;
    }
    
    int main(void)
    {
        char ch;
        int cnt = 0, ans = 0;
        while ((ch = getchar()) != '
    ') {
            if (ch == 'a') cnt++;
            else {
                ans += pwr(2, cnt) - 1;
                ans %= MOD;
            }
        }
        printf("%d
    ", ans);
    }
  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/siuginhung/p/7723003.html
Copyright © 2011-2022 走看看