結果

問題 No.295 hel__world
ユーザー lam6er
提出日時 2025-03-31 17:56:10
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,866 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 82,712 KB
実行使用メモリ 167,396 KB
最終ジャッジ日時 2025-03-31 17:57:16
合計ジャッジ時間 5,896 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    from collections import defaultdict

    # Read input
    s_alpha = list(map(int, sys.stdin.readline().split()))
    T = sys.stdin.readline().strip()

    # Step 1: Decompose T into contiguous components C
    C = []
    prev_char = None
    for char in T:
        if char != prev_char:
            C.append(char)
            prev_char = char
    k = len(C)
    if k == 0:
        print(0)
        return

    # Step 2: Check if S can be formed
    char_indices = defaultdict(list)
    for idx, char in enumerate(C):
        char_indices[char].append(idx)

    # Check if each character's count in C is <= S_alpha
    valid = True
    for char in char_indices:
        cnt = len(char_indices[char])
        alpha_index = ord(char) - ord('a')
        if s_alpha[alpha_index] < cnt:
            valid = False
            break
    if not valid:
        print(0)
        return

    # Initialize m array with 1s
    m = [1] * k

    # Step 3: Distribute remaining characters for each alpha
    for char in char_indices:
        indices = char_indices[char]
        cnt = len(indices)
        alpha_index = ord(char) - ord('a')
        s_count = s_alpha[alpha_index]
        rem = s_count - cnt
        if rem <= 0:
            continue

        q, r = divmod(rem, cnt)
        for i in range(len(indices)):
            pos = indices[i]
            add = q
            if i < r:
                add += 1
            m[pos] += add

    # Step 4: Compute the product and check overflow
    max_val = (1 << 62)
    product = 1
    for num in m:
        if num == 0:
            print(0)
            return
        if product > max_val // num:
            print("hel")
            return
        product *= num
        if product >= max_val:
            print("hel")
            return
    print(product)

if __name__ == "__main__":
    main()
0