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")
1.
你的 empty 是想紀錄連續的空床位嗎?
考慮下面這兩種情況: "X....X", "X..."
你的邏輯是遇到 X 就 append,所以當X出現在第一個時,就可能出現錯誤的陣列
2.
判斷空床位的部分,第一個 if
if len(beds) == 1: |
就算空床位都集中在某一側,也不會永遠都是 1
例如 '......X' ,人躺在最左側就可以隔 5 個空位,應輸出 5
3.
一樣是判斷空床位的邏輯
if beds[0] == '.': |
和第二點一樣,拿 '......X' 為例,人躺在最左側,答案應該是「連續的空床位數量 - 1」
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")