【问题标题】:Need help understanding USACO solution需要帮助了解 USACO 解决方案
【发布时间】:2019-07-29 21:05:36
【问题描述】:

我试图解决 USACO 的项链断裂问题,我遇到了这个解决方案。问题陈述在这里:https://train.usaco.org/usacoprob2?S=beads&a=c3sjno1crwH

我很困惑为什么写这个解决方案的人复制了 3 个初始字符串,基本上是整个 for 循环。

我已经尝试在网上寻找其他可能更好地解释它的解决方案,但是对于这个问题有少量的 python 解决方案,其中许多是完全不同的。

'''
ID: krishpa2
LANG: PYTHON3
TASK: beads
'''



with open('beads.in','r') as fin:
    N = int(fin.readline())
    beads = fin.readline()[:-1]

def canCollect(s):
    return not ('r' in s and 'b' in s)

beads = beads*3
max = 0

for p in range(N, N*2):
    i = p-1
    left = []
    while i > 0:
        if canCollect(left + [beads[i]]):
            left.append(beads[i])
            i -= 1
        else:
            break

    i = p
    right = []
    while i < 3*N - 1:
        if canCollect(right + [beads[i]]):
            right.append(beads[i])
            i+=1
        else:
            break

    result = len(left) + len(right)
    if result >= N:
        max = N
        break
    elif result > max:
        max = result
print(max)
with open('beads.out','w') as fout:
    fout.write(str(max) + '\n')

程序运行正常,我只是想知道为什么。

【问题讨论】:

  • 尝试在this visualizer 中复制您的代码,然后自己查看代码的运行步骤。任何人都可以在这里解释一下会更好......
  • 这里的解决方案broken necklace简单来说你需要连续找到每个珠子的频率,即有多少相同颜色的珠子在一起,一次取2个颜色频率,总和大的两个是你的解决方案
  • @prashantrana 我查看了该解决方案,但如果珠子 =“rrr”,它会使 USACO 分级机失败。编辑:我添加了一个子句,如果珠子 == 珠子 [::-1],它返回珠子的长度。我提交了该解决方案,它通过了 USACO 分级机。
  • 酷,恭喜,有任何问题都可以到 GitHub 寻找代码解决方案

标签: python arrays python-3.x loops


【解决方案1】:

我知道这个问题已经很老了,但我还是想为以后的人回答这个问题,所以我在下面做了一个完整的评论版本(基于 joshjq91 的this 回答)-

"""
PROG: beads
LANG: PYTHON3
#FILE
"""

# Original file from Github by joshjq91 
# (https://github.com/jadeGeist/USACO/blob/master/1.2.4-beads.py)
# Comments by Ayush

with open('beads.in','r') as filein:
    N = int(filein.readline()) # Number of beads
    beads = filein.readline()[:-1] # Necklace

def canCollect(s):
    return not ('r' in s and 'b' in s) # If r and b are not in the same str,
                                       # then you can collect the string.
beads = beads*3 # Wraparound - r actually can be shown as r r r (wraparound 
                # for the front and back)
max = 0 # The final result

for p in range(N, N*2): # Loop through the 2nd bead string (so you can use
    i = p-1             # wraparounds for the front and back)
    left = []
    while i > 0: # Check if you can collect beads (left)
        if canCollect(left + [beads[i]]): # Can colleect
            left.append(beads[i]) # Add to left
            i -= 1 # Loop through again
        else:
            break # Cannot collect more beads - break

    i = p # You will otherwise have a duplicate bead (left is i=p-1)
    right = []
    while i < 3*N - 1:        # Check if you can collect beads (right) - i has
        #print("righti",i-N)  # to be less than 3*N - 1 b/c that is the length
        # ^ for testing       # of the beads + runarounds.

        if canCollect(right + [beads[i]]): # Can collect
            right.append(beads[i]) # Add to right
            i+=1 # Loop through again
        else:
            break # Cannot collect more beads - break

    result = len(left) + len(right) # Final result
    if result >= N: # The result was greater than N means that the whole
        max = N     # necklace is the same (EX: rwr)
        break # Break - we now know we don't need to go through again b/c the
              # whole string is the same! 
    elif result > max: # The result makes sense
        max = result

with open('beads.out','w') as fileout:
    fileout.write(str(max) + '\n') # Final result

【讨论】:

    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多