Problem p02937 (python)

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from bisect import bisect_left

s = input().strip()  # e.g., "contest"
t = input().strip()  # e.g., "son"
# s = "contest"
# t = "son"
# t = "programming"
# t = "sentence"

# 文字列 s 二周分の文字の出現位置マップを作る
s_map = [[] for _ in range(26)]
for i, ch in enumerate((s + s)):
    s_map[ord(ch) - ord('a')].append(i)

# t を舐めながら回答を作る
p = ans = 0
for ch in t:
    ch_position_list = s_map[ord(ch) - ord('a')]
    if len(ch_position_list) == 0:
        print('-1')
        sys.exit()

    p = ch_position_list[bisect_left(ch_position_list, p)] + 1
    if len(s) <= p:
        ans += len(s)
        p -= len(s)

ans += p
print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from collections import deque
import bisect
import sys

# 準備
idx = 0
charcode_mapper = {}
for ch in "abcdefghijklmnopqrstuvwxyz":
    charcode_mapper[ch] = idx
    idx += 1

s = input().strip()  # e.g., "contest"
t = input().strip()  # e.g., "son"
# s = "contest"
# t = "son"
# t = "programming"
# t = "sentence"

# s 中の文字の出現順序を保持する (本当は一行でできそう)
# こういうリストを作る (イメージ)
# [
#     [x, y],  # a の出現位置
#     [z],     # b の出現位置
#     ...,
#     [i, j]   # z の出現位置
# ]
char_idx = 0
char_positions = [[] for _ in range(26)]  # 26文字分


def extend_char_positions():
    global char_idx
    for ch in s:
        char_positions[charcode_mapper[ch]].append(char_idx)
        char_idx += 1


extend_char_positions()
extend_char_positions()

# print(charcode_mapper)
# print(char_positions)

# idx を 0 とする
# t の文字をなめていき、char_positions の idx より先にその文字が存在するかどうか確認する
#   あれば)
#     idx をそこまで進める
#     `(len(s)  - idx) < len(t)` であれば char_positions を伸ばす
#   なければ)
#     return -1

idx = 0
for ch in t:
    charcode = charcode_mapper[ch]
    idx = bisect.bisect_left(
        char_positions[charcode],
        idx,
        0,
        len(char_positions[charcode])
    )
    # print(ch)  # s
    # print(char_positions[charcode])  # [5, 12]
    try:
        idx = char_positions[charcode][idx]
    except Exception:
        print('-1')
        sys.exit(1)
    # print(idx)
    if len(s) - idx < len(t):
        extend_char_positions()


print(idx + 1)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from collections import deque
import bisect
import sys

# 準備
idx = 0
charcode_mapper = {}
for ch in "abcdefghijklmnopqrstuvwxyz":
    charcode_mapper[ch] = idx
    idx += 1

s = input().strip()  # e.g., "contest"
t = input().strip()  # e.g., "son"
# s = "contest"
# t = "son"
# t = "programming"
# t = "sentence"

# s 中の文字の出現順序を保持する (本当は一行でできそう)
# こういうリストを作る (イメージ)
# [
#     [x, y],  # a の出現位置
#     [z],     # b の出現位置
#     ...,
#     [i, j]   # z の出現位置
# ]
char_idx = 0
char_positions = [[] for _ in range(26)]  # 26文字分


def extend_char_positions():
    global char_idx
    for ch in s:
        char_positions[charcode_mapper[ch]].append(char_idx)
        char_idx += 1


extend_char_positions()
extend_char_positions()

# print(charcode_mapper)
# print(char_positions)

# idx を 0 とする
# t の文字をなめていき、char_positions の idx より先にその文字が存在するかどうか確認する
#   あれば)
#     idx をそこまで進める
#     `(len(s)  - idx) < len(t)` であれば char_positions を伸ばす
#   なければ)
#     return -1

idx = 0
for ch in t:
    charcode = charcode_mapper[ch]
    idx = bisect.bisect_left(
        char_positions[charcode],
        idx,
        0,
        len(char_positions[charcode])
    )
    # print(ch)  # s
    # print(char_positions[charcode])  # [5, 12]
    try:
        idx = char_positions[charcode][idx]
    except IndexError:
        print('-1')
        sys.exit(1)
    # print(idx)
    if len(s) - idx < len(t):
        extend_char_positions()


print(idx + 1)