結果

問題 No.996 Phnom Penh
ユーザー gew1fw
提出日時 2025-06-12 15:08:31
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,469 bytes
コンパイル時間 419 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 112,180 KB
最終ジャッジ日時 2025-06-12 15:09:14
合計ジャッジ時間 4,414 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

def max_operations(s):
    s_list = list(s)
    n = len(s_list)
    count = 0
    while True:
        # Try Operation 1
        phnom_pos = []
        for i in range(n - 4):
            if ''.join(s_list[i:i+5]) == 'phnom':
                phnom_pos.append(i)
        if phnom_pos:
            # Perform Operation 1 on the first occurrence
            for pos in phnom_pos:
                s_list[pos:pos+5] = list('penh')
                count += 1
                # After replacement, break and restart
                n = len(s_list)
                break
            continue  # Restart the loop after Operation 1

        # Try Operation 2
        # Step 1: Remove all 'h's
        new_s = []
        has_h = False
        for c in s_list:
            if c == 'h':
                has_h = True
            else:
                new_s.append(c)
        if has_h:
            # Step 2: Replace 'e's with 'h's
            has_e = False
            for i in range(len(new_s)):
                if new_s[i] == 'e':
                    new_s[i] = 'h'
                    has_e = True
            # Check if the string changed
            changed = has_h or has_e
            if changed:
                s_list = new_s
                count += 1
                n = len(s_list)
                continue
        # If no operations can be performed
        break
    
    return count

# Read input
s = input().strip()
# Compute and print the result
print(max_operations(s))
0