#45029: python求救


hansjiang1017@gmail.com (單純想出題所以在拚30%)

學校 : 不指定學校
編號 : 278037
來源 : [111.242.81.197]
最後登入時間 :
2025-05-14 20:42:17
m851. 13181 Sleeping in hostels -- UVA | From: [111.242.93.217] | 發表日期 : 2025-01-04 11:05

import sys
result = []
for s in sys.stdin:
    beds = s.strip()
    empty = []
    count = 0
    for bed in beds:
        if bed == "X":
            empty.append(count)
            count = 0
        else:
            count += 1
    if count != 0:
        empty.append(count)
    max_d = 0
    if len(beds) == 1:
        result.append("1")
    else:
            
        if beds[0] == '.': 
            max_d = max(max_d, empty[0])
        if beds[-1] == '.' and len(empty) > 1:
            max_d = max(max_d, empty[-1] - 1)

        for dist in empty[1:-1]:
            max_d = max(max_d, (dist - 1) // 2)

        result.append(str(max_d))
sys.stdout.write("\n".join(result)+"\n")

 
#45094: Re: python求救


sam851015@gmail.com (多挖鼻孔有益身心健康)

學校 : 臺中市立惠文高級中學
編號 : 277705
來源 : [123.192.228.253]
最後登入時間 :
2025-05-10 17:41:09
m851. 13181 Sleeping in hostels -- UVA | From: [123.192.228.253] | 發表日期 : 2025-01-06 18:55

1.

你的 empty 是想紀錄連續的空床位嗎?

考慮下面這兩種情況: "X....X", "X..."

你的邏輯是遇到 X 就 append,所以當X出現在第一個時,就可能出現錯誤的陣列

 

2.

判斷空床位的部分,第一個 if

if len(beds) == 1:
    result.append("1")

 

就算空床位都集中在某一側,也不會永遠都是 1

例如 '......X' ,人躺在最左側就可以隔 5 個空位,應輸出 5

 

3.

一樣是判斷空床位的邏輯

if beds[0] == '.':
    max_d = max(max_d, empty[0])

 

和第二點一樣,拿 '......X' 為例,人躺在最左側,答案應該是「連續的空床位數量 - 1」

 

 
#45129: Re: python求救


hansjiang1017@gmail.com (單純想出題所以在拚30%)

學校 : 不指定學校
編號 : 278037
來源 : [111.242.81.197]
最後登入時間 :
2025-05-14 20:42:17
m851. 13181 Sleeping in hostels -- UVA | From: [111.242.124.68] | 發表日期 : 2025-01-10 22:08

1.

你的 empty 是想紀錄連續的空床位嗎?

考慮下面這兩種情況: "X....X", "X..."

你的邏輯是遇到 X 就 append,所以當X出現在第一個時,就可能出現錯誤的陣列

 

2.

判斷空床位的部分,第一個 if

if len(beds) == 1:
    result.append("1")

 

就算空床位都集中在某一側,也不會永遠都是 1

例如 '......X' ,人躺在最左側就可以隔 5 個空位,應輸出 5

 

3.

一樣是判斷空床位的邏輯

if beds[0] == '.':
    max_d = max(max_d, empty[0])

 

和第二點一樣,拿 '......X' 為例,人躺在最左側,答案應該是「連續的空床位數量 - 1」

 

謝謝幫忙,AC了

import sys
result = []
for s in sys.stdin:
    beds = s.strip()
    empty = []
    count = 0
    for bed in beds:
        if bed == "X":
            empty.append(count)
            count = 0
        else:
            count += 1
    if count != 0:
        empty.append(count)
    if empty[0] == 0:
        empty.pop(0)
    max_d = 0
    if beds[0] == '.': 
        max_d = max(max_d, empty.pop(0) - 1)
    if beds[-1] == '.':
        max_d = max(max_d, empty.pop() - 1)

    for dist in empty:
        max_d = max(max_d, (dist - 1) // 2)

    result.append(str(max_d))
sys.stdout.write("\n".join(result)+"\n")



 
ZeroJudge Forum