zoukankan      html  css  js  c++  java
  • Kick Start 2018-Round H-Problem A. Big Buttons

    Problem

    You are a contestant on a popular new game show and are playing for the grand prize!

    There are two big buttons, a red one and a black one. You will make a sequence of exactly N button presses.

    There are lots of different sequences of presses you could make, but there are P forbidden prefixes, each of length no greater than N. If you make a sequence of presses which begins with any of the forbidden sequences, you will not win the grand prize. It is fine for your sequence to contain one or more forbidden prefixes as long as they do not appear at the start of your sequence.

    A winning sequence must consist of exactly N button presses and must not begin with one of the forbidden prefixes. How many different winning sequences are there?
    Input

    The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing two integers N and P, as described above. Then, there are P more lines, each of which contains a string of between 1 and N characters, inclusive, describing one of the forbidden sequences of presses. An R represents pressing the red button, whereas a B represents pressing the black button.
    Output

    For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of winning sequences, as desribed above.
    Limits

    1 ≤ T ≤ 100.
    1 ≤ P ≤ min(2N, 100).
    Each forbidden prefix is between 1 and N characters long, inclusive.
    No two forbidden prefixes will be the same.
    Small dataset

    1 ≤ N ≤ 10.
    Large dataset

    1 ≤ N ≤ 50.
    Sample

    Input

    4
    3 2
    BBB
    RB
    5 1
    R
    4 3
    R
    B
    RBRB
    50 5
    BRBRBBBRBRRRBBB
    BRBRBRRRBRRRBRB
    BBBRBBBRBRRRBBB
    BRBRBRRRBRRRB
    BRBRBBBRBBBRB

    output:
    Case #1: 5
    Case #2: 16
    Case #3: 0
    Case #4: 1125556309458944

    Note that the last Sample case would not appear in the Small dataset.

    In the first case, you must make a sequence of 3 presses. There are 8 possible sequences of three presses, but some of them will cause you to lose the game. They are listed below:

    • RBB. This is forbidden since it starts with the first forbidden sequence (RB).
    • RBR. This is forbidden since it starts with the first forbidden sequence (RB).
    • BBB. This is forbidden since it starts with the second forbidden sequence (BBB).

    Thus, there are only 5 winning sequences.

    In the second case, you must make a sequence of 5 presses. There is only one forbidden sequence, which is R. This means that the first press must be B, and the next 4 presses can be either button. This gives a total of 16 different button presses.

    In the third case, you must make a sequence of 4 presses. There are three forbidden sequences, but since every possible sequence begins with either R (the first forbidden sequence) or B (the second forbidden sequence), there are no winning sequences. So the answer is 0.

    先上解法

    t = int(input())
    for case in range(t):
        [n,p] = input().split(' ')
        n = int(n)
        p = int(p)
        # print(n,p)
        l = []
        for i in range(p):
            s = input()
            l.append(s)
        l.sort(key = lambda x:len(x))
        # print(l)
        p,q = 0,len(l)-1
        while p<len(l)-1:
            while q>p:
                if l[q].startswith(l[p]):
                    l.pop(q)
                    q -= 1
                else:
                    q -= 1
            p += 1
            q = len(l)-1
        # print(l)
        count = 2**n
        for i in range(len(l)):
            le = len(l[i])
            count -= 2**(n-le)
        print("Case #",end='')
        print(case+1,end='')
        print(": ",end='')
        print(count)
    

    很简单的一道题,首先按照长度排序,然后看是否有哪个字符串是另一个字符串的头。不过读题读了很长时间,然后做了大概半个小时吧。

  • 相关阅读:
    漫谈LiteOS-端云互通组件-MQTT开发指南(下)
    漫谈LiteOS-端云互通组件-MQTT开发指南(上)
    漫谈LiteOS之开发板-LiteOS移植(基于GD32450i-EVAL)
    漫谈LiteOS-Huawei_IoT_Link_SDK_OTA 开发指导
    SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 实现分库分表
    从零开始实现放置游戏(十五)——实现战斗挂机(6)在线打怪练级
    Windows系统安装最新版本RabbitMQ3.8.3报错解决
    从零开始实现放置游戏(十四)——实现战斗挂机(5)地图移动和聊天
    从零开始实现放置游戏(十二)——实现战斗挂机(3)数据字典和缓存改造
    从零开始实现放置游戏(十一)——实现战斗挂机(2)注册登陆和游戏主界面
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/10051077.html
Copyright © 2011-2022 走看看