Problem: Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.    * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Submission attempts (in order):
# -*- coding: utf-8 -*-


def main():
    s = input().splitlines()[0]
    t = input()

    Slen = len(s)

    LookupTable = [[] for i in range(26)]
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        LookupTable[v].append(i)
    for i in range(26):
        LookupTable[i].append(Slen)
        
    IndexTable = [0 for i in range(26)]
    NextTable = []
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        if LookupTable[v][IndexTable[v]] >= i:
            IndexTable[v] += 1
        if LookupTable[v][IndexTable[v]] == Slen:
            IndexTable[v] = 0
    NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        
    Index = 0
    Loop = 0
    for i, c in enumerate(t):
        v = ord(c) - ord('a')
        if NextTable[Index][v] >= Index:
            Loop += 1
        Index = NextTable[Index][v]

    if Loop == Slen:
        print(-1)
    else:
        print(Loop*Slen+Index+1)


if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-


def main():
    s = input().splitlines()[0]
    t = input()

    Slen = len(s)

    LookupTable = [[] for i in range(26)]
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        LookupTable[v].append(i)
    for i in range(26):
        LookupTable[i].append(Slen)
        
    IndexTable = [0 for i in range(26)]
    NextTable = []
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        if LookupTable[v][IndexTable[v]] >= i:
            IndexTable[v] += 1
        if LookupTable[v][IndexTable[v]] == Slen:
            IndexTable[v] = 0
        
    Index = 0
    Loop = 0
    for i, c in enumerate(t):
        v = ord(c) - ord('a')
        if NextTable[Index][v] >= Index:
            Loop += 1
        Index = NextTable[Index][v]
        if Index == Slen:
            print(-1)
            return

    print(Loop*Slen+Index+1)


if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-


def main():
    s = input().splitlines()[0]
    t = input().splitlines()[0]

    Slen = len(s)

    LookupTable = [[] for i in range(26)]
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        LookupTable[v].append(i)
    for i in range(26):
        LookupTable[i].append(-1)
        
    IndexTable = [0 for i in range(26)]
    NextTable = []
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        IndexTable[v] += 1
        if LookupTable[v][IndexTable[v]] < 0:
            IndexTable[v] = 0

    Index = 0
    Loop = 0
    for i, c in enumerate(t):
        v = ord(c) - ord('a')
        if NextTable[Index][v] < Index:
            Loop += 1
        Index = NextTable[Index][v]
        if Index < 0:
            print(-1)
            return
        Index += 1
        if Index >= Slen:
            Index = 0
            Loop += 1

    print(Loop*Slen+Index)


if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Wrong Answer

Accepted