Problem p02937 (python)

Fixed submission:
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def solve(positions, s, t):

    if not set(t) <= set(s):
        return -1

    loop = 0
    prev = -1

    for tchar in t:
        candidates = positions[tchar]
        pos = bisect.bisect_right(candidates, prev)

        if pos == len(candidates):
            loop += 1
            pos = 0
        prev = candidates[pos]

    return loop * len(s) + prev + 1


def main():
    s = input()
    t = input()

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def solve(positions, s, t):
    # print('{}, s = {}, t = {}'.format(positions, s, t))
    if len(t) == 0:
        return 0

    if len(positions[t[0]]) == 0:
        return -1

    i = 0
    j = 0
    while i < len(s) and j < len(t):
        candidates = positions[t[j]]
        pos = bisect.bisect_left(candidates, i)
        if pos == len(candidates):
            print('{} {}'.format(t[j], pos))
            break

        i = candidates[pos] + 1
        j += 1

    if j == len(t):
        return i
    else:
        rest = solve(positions, s, t[j:])
        return len(s) + rest if rest != -1 else -1


def main():
    s = input()
    t = input()

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def solve(positions, s, t):
    # print('{}, s = {}, t = {}'.format(positions, s, t))
    if len(t) == 0:
        return 0

    if len(positions[t[0]]) == 0:
        return -1

    i = 0
    j = 0
    while i < len(s) and j < len(t):
        candidates = positions[t[j]]
        if len(candidates) == 0:
            break
        pos = bisect.bisect_left(candidates, i)
        if pos == len(candidates):
            break

        i = candidates[pos] + 1
        j += 1

    if j == len(t):
        return i
    else:
        rest = solve(positions, s, t[j:])
        return len(s) + rest if rest != -1 else -1


def main():
    s = input()
    t = input()

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def solve(positions, s, t):
    # print('{}, s = {}, t = {}'.format(positions, s, t))
    if len(t) == 0:
        return 0
    if len(positions[t[0]]) == 0:
        return -1

    i = 0
    j = 0
    while i < len(s) and j < len(t):
        candidates = positions[t[j]]
        pos = bisect.bisect_left(candidates, i)
        if pos == len(candidates):
            break
        # print('pos = {} t = {}, i = {}'.format(candidates[pos], t[j], i))
        i = candidates[pos] + 1
        j += 1

    if j == len(t):
        return i
    else:
        rest = solve(positions, s, t[j:])
        return len(s) + rest if rest != -1 else -1


def main():
    s = input()
    t = input()

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    # check can or cannot
    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def solve(positions, s, t):
    # print('{}, s = {}, t = {}'.format(positions, s, t))
    if len(t) == 0:
        return 0
    if len(positions[t[0]]) == 0:
        return -1

    i = 0
    j = 0
    while i < len(s) and j < len(t):
        pos = list(filter(lambda idx: idx >= i, positions[t[j]]))
        # print('pos = {} t = {}, i = {}'.format(pos, t[j], i))
        if len(pos) == 0:
            break
        i = pos[0] + 1
        j += 1

    if j == len(t):
        return i
    else:
        rest = solve(positions, s, t[j:])
        return len(s) + rest if rest != -1 else -1


def main():
    s = input()
    t = input()

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    # check can or cannot
    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def solve(s, t):
    # print('{} {}'.format(s, t))
    if len(t) == 0:
        return 0
    if s.find(t[0]) == -1:
        return -1
    i = 0
    j = 0
    while i < len(s) and j < len(t):
        target = s[i:]
        pos = target.find(t[j])
        # print('pos = {} t = {}'.format(pos, t[j]))
        if pos == -1:
            break
        i += pos + 1
        j += 1

    if j == len(t):
        return i
    else:
        rest = solve(s, t[j:])
        return len(s) + rest if rest != -1 else -1


def main():
    s = input()
    t = input()

    # check can or cannot
    print(solve(s * 10, t))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def solve(s, t):
    # print('{} {}'.format(s, t))
    if len(t) == 0:
        return 0
    if s.find(t[0]) == -1:
        return -1
    i = 0
    j = 0
    while i < len(s) and j < len(t):
        target = s[i:]
        pos = target.find(t[j])
        # print('pos = {} t = {}'.format(pos, t[j]))
        if pos == -1:
            break
        i += pos + 1
        j += 1

    if j == len(t):
        return i
    else:
        rest = solve(s, t[j:])
        return len(s) + rest if rest != -1 else -1

def main():
    s = input()
    t = input()

    # check can or cannot
    print(solve(s, t))


if __name__ == '__main__':
    main()