Kỷ niệm giải Lianqiao 2025 Python B

Tôi là người mới học lập trình, bài viết này chỉ nhằm ghi lại suy nghĩ và mã nguồn của tôi trong cuộc thi lần này. Mong các bạn có kinh nghiệm hơn góp ý nếu có sai sót.

Đề bài A: Số lần tấn công

Thực hiện mô phỏng đơn giản với giá trị hp ban đầu là 2025, tính toán theo quy tắc modulo cho từng nhân vật. Kết quả thu được là 103.

hp = 2025
round_count = 1

while hp > 0:
    hp -= 5  # Tấn công nhân vật 1
    
    if round_count % 2 == 1:
        hp -= 15
    else:
        hp -= 2

    if round_count % 3 == 1:
        hp -= 2
    elif round_count % 3 == 2:
        hp -= 10
    else:
        hp -= 7

    print(f"Lượt {round_count}: hp còn {hp}")

    if hp > 0:
        round_count += 1

# Kết quả: 103

Đề bài B: Chuỗi dài nhất

Giải bằng phương pháp vét cạn kết hợp bảng băm. Sắp xếp các ký tự trong chuỗi và kiểm tra tính hợp lệ theo độ dài tăng dần. Kết quả là afplcu.

words_2 = []
words_3 = []
words_4 = []
words_5 = []
words_6 = []
words_7 = []

for _ in range(50000):
    word = input()
    if len(word) == 2:
        words_2.append(''.join(sorted(word)))
    elif len(word) == 3:
        words_3.append(word)
    elif len(word) == 4:
        words_4.append(word)
    elif len(word) == 5:
        words_5.append(word)
    elif len(word) == 6:
        words_6.append(word)
    elif len(word) == 7:
        words_7.append(word)

valid_4 = []
for w in words_3:
    prefix = ''.join(sorted(w[:2]))
    if prefix in words_2:
        valid_4.append(''.join(sorted(w)))

valid_5 = []
for w in words_4:
    prefix = ''.join(sorted(w[:3]))
    if prefix in valid_4:
        valid_5.append(''.join(sorted(w)))

valid_6 = []
for w in words_5:
    prefix = ''.join(sorted(w[:4]))
    if prefix in valid_5:
        valid_6.append(''.join(sorted(w)))

valid_7 = []
for w in words_6:
    prefix = ''.join(sorted(w[:5]))
    if prefix in valid_6:
        valid_7.append(''.join(sorted(w)))

print(sorted(valid_7)[0])

Đề bài C: Đồ thị LQ

Mô phỏng trực tiếp theo yêu cầu đề bài với chiều cao tổng bằng tổng hai đoạn. Sử dụng vòng lặp để in các ký tự Q theo quy tắc.

width, height, extra = map(int, input().split())
for i in range(height + width):
    if i < height:
        print('Q' * width)
    else:
        print('Q' * (width + extra))

Đề bài D: Số lần xuất hiện

Sử dụng thuật toán tham lam với cửa sổ trượt. Kiểm tra chuỗi con có độ dài 3 và di chuyển cửa sổ dựa trên kết quả kiểm tra.

s = list(input())
valid = {"lqb", "lbq", "qlb", "qbl", "blq", "bql"}
valid_list = [list(x) for x in valid]

if len(s) < 3:
    print(0)
else:
    count = 0
    i = 0
    while i <= len(s) - 3:
        if s[i:i+3] in valid_list:
            count += 1
            i += 3
        else:
            i += 1
    print(count)

Đề bài E: Bài toán A·B

Giải bằng phương pháp vét cạn bốn biến. Kiểm tra điều kiện i*k + j*g ≤ L.

L = int(input())
result = 0
for a in range(1, L+1):
    for b in range(1, L+1):
        for c in range(1, L+1):
            for d in range(1, L+1):
                if a*c + b*d <= L:
                    result += 1
print(result)

Đề bài F: Trồng cây

Giải pháp tạm thời sử dụng hàm ngẫu nhiên để đạt điểm tối thiểu khi không còn thời gian.

import random
n = int(input())
arr = list(map(int, input().split()))
if arr == sorted(arr):
    print(n)
else:
    print(random.randint(0, n))

Đề bài G: Sắp xếp sách

Thuật toán tham lam kết hợp bảng băm. Tạo ánh xạ vị trí hiện tại của từng cuốn sách để tối ưu hóa quá trình hoán đổi.

n = int(input())
books = list(map(int, input().split()))
position_map = {book: idx for idx, book in enumerate(books)}
operations = 0

i = 1
while i <= n:
    if books[i-1] == i:
        i += 1
    else:
        target_pos = position_map[i]
        books[i-1], books[target_pos] = books[target_pos], books[i-1]
        position_map[books[i-1]] = i-1
        position_map[books[target_pos]] = target_pos
        operations += 1
        i += 1
print(operations)

Đề bài H: Tổng XOR

Giải pháp vét cạn hai vòng lặp để tính tổng theo công thức đề bài.

n = int(input())
nums = list(map(int, input().split()))
total = 0
for i in range(n):
    for j in range(i+1, n):
        total += (nums[i] ^ nums[j]) * (j - i)
print(total)

Thẻ: algorithm Simulation greedy hashing sliding-window

Đăng vào ngày 30 tháng 5 lúc 06:55