Problem:


Score : 500 points

Problem Statement

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').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.



Sample input:
contest
son


Sample output:
10

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_right

s = input()
t = input()
idxs = defaultdict(list)

for i, c in enumerate(s):
    idxs[c].append(i)
for i, c in enumerate(s):
    idxs[c].append(len(s) + i)

ans, cur_i = 0, 0
for c in t:
    if c not in idxs: print(-1); break
    a = idxs[c]
    nxt = a[bisect_right(a, cur_i)]
    ans += nxt - cur_i
    cur_i = nxt % len(s)
else: print(ans+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left

s = input()
t = input()
idxs = defaultdict(list)

for i, c in enumerate(s):
    idxs[c].append(i)
for i, c in enumerate(s):
    idxs[c].append(len(s) + i)

ans, cur_i = 0, 0
for c in t:
    if c not in idxs: print(-1); break
    a = idxs[c]
    nxt = a[bisect_left(a, cur_i)]
    ans += nxt - cur_i
    cur_i = nxt % len(s)
else: print(ans+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left

s = input()
t = input()
idxs = defaultdict(list)

for i, c in enumerate(s):
    idxs[c].extend([len(s) * j + i for j in range(len(t))])

for v in idxs.values(): v.sort()

ans = 0

for c in t:
    if c not in idxs: print(-1); break
    a = idxs[c]
    ans = a[bisect_left(a, ans)]
else: print(ans+1)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

S = str(input())
T = str(input())
N = len(S)
cou = int(0)
cur = int(0)
move = int(0)
S_map = [[]for _ in range(26)]

for i in range(N):
    S_map[ord(S[i])-97].append(i+1)
for i in range(N):
    S_map[ord(S[i])-97].append(N+i+1)

for c in T:
    print(cur)
    if S_map[ord(c)-97]==[]:
        ans = -1
        break
    move = bisect.bisect_left(S_map[ord(c)-97],cur+1)
    cur = S_map[ord(c)-97][move]
    if cur>=N:
        cou += 1
        cur -= N

if ans==0:
    ans = cou*N + cur

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

S = str(input())
T = str(input())
N = len(S)
cou = int(0)
cur = int(0)
move = int(0)
S_map = [[]for _ in range(26)]

for i in range(N):
    S_map[ord(S[i])-97].append(i+1)
for i in range(N):
    S_map[ord(S[i])-97].append(N+i+1)

for c in T:
    if S_map[ord(c)-97]==[]:
        ans = -1
        break
    move = bisect.bisect_left(S_map[ord(c)-97],cur)
    cur = S_map[ord(c)-97][move]
    if cur>=N:
        cou += 1
        cur -= N

if ans==0:
    ans = cou*N + cur

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_map = [[] for _ in range(26)]
import bisect
for i in range(len(s)):
    s_map[ord(s[i])-97].append(i)
for i in range(len(s)):
    s_map[ord(s[i])-97].append(len(s)+i)
cur = 0
count = 0
f = True
for c in t:
    if s_map[ord(c)-97] == []:
        f = False
        break
    np = bisect.bisect_left(s_map[ord(c)-97],cur)
    if s_map[ord(c)-97][np] == cur:
        cur = s_map[ord(c)-97][np+1]
    else:
        cur = s_map[ord(c)-97][np]
    if cur >= len(s):
        count += 1
        cur -= len(s)
print(count * len(s) + cur+1 if f else -1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_map = [[] for _ in range(26)]
import bisect
for i in range(len(s)):
    s_map[ord(s[i])-97].append(i)
for i in range(len(s)):
    s_map[ord(s[i])-97].append(len(s)+i)
cur = 0
count = 0
f = True
for c in t:
    if s_map[ord(c)-97] == []:
        f = False
        break
    cur = s_map[ord(c)-97][bisect.bisect_left(s_map[ord(c)-97],cur)]
    if cur >= len(s):
        count += 1
        cur -= len(s)
print(count * len(s) + cur+1 if f else -1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_map = [[] for _ in range(26)]
import bisect
for i in range(len(s)):
    s_map[ord(s[i])-97].append(i)
for i in range(len(s)):
    s_map[ord(s[i])-97].append(len(s)+i)
cur = 0
count = 0
f = True
for c in t:
    if s_map[ord(c)-97] == []:
        f = False
        break
    cur = s_map[ord(c)-97][bisect.bisect_left(s_map[ord(c)-97],cur)]
    print(cur)
    if cur >= len(s):
        count += 1
        cur -= len(s)
print(count * len(s) + cur+1 if f else -1)

Submission status: Wrong Answer

Solution code:
from collections import deque
from collections import defaultdict

s = input()
t = input()
n = len(s)
indexes = defaultdict(list)
queues = defaultdict(deque)

for i, x in enumerate(s):
    indexes[x].append(i)
    queues[x].append(i)

ans = 0
for x in t:
    if x not in indexes: print(-1); exit()
    i = 0
    cnt = 1
    while i <= ans:
        if len(queues[x]) == 0:
            for j in indexes[x]:
                queues[x].append(j+n*cnt)
            cnt += 1
        i = queues[x].popleft()
    ans = i

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys


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

    appear_pos = {}  # 文字が出てくる場所
    for idx, T in enumerate(t):
        if T not in appear_pos:
            appear_pos[T] = [idx]
        else:
            appear_pos[T].append(idx)

    search_idx = {}
    for key in appear_pos.keys():
        search_idx[key] = 0

    loop_count = 0
    find_idx = -1
    for S in s:
        if S not in appear_pos:
            print(-1)
            sys.exit()

        if len(appear_pos[S]) <= search_idx[S]:
            # 一周している時
            loop_count += 1
            for key in appear_pos.keys():
                search_idx[key] = 0
            find_idx = appear_pos[S][0]
            search_idx[S] = 1
            continue

        idx_new = appear_pos[S][search_idx[S]]
        search_idx[S] += 1

        if idx_new <= find_idx:
            # これも一周している時
            loop_count += 1
            for key in appear_pos.keys():
                search_idx[key] = 0

        find_idx = idx_new

    print(find_idx+loop_count*len(t)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

    appear_pos = {}  # 文字が出てくる場所
    for idx, T in enumerate(t):
        if T not in appear_pos:
            appear_pos[T] = [idx]
        else:
            appear_pos[T].append(idx)

    search_idx = {}
    for key in appear_pos.keys():
        search_idx[key] = 0

    loop_count = 0
    find_idx = 0
    for S in s:
        if S not in appear_pos:
            print(-1)
            sys.exit()

        if len(appear_pos[S]) <= search_idx[S]:
            # 一周している時
            loop_count += 1
            for key in appear_pos.keys():
                search_idx[key] = 0
            find_idx = appear_pos[S][0]
            search_idx[S] = 1
            continue

        idx_new = appear_pos[S][search_idx[S]]
        search_idx[S] += 1

        if idx_new <= find_idx:
            # これも一周している時
            loop_count += 1
            for key in appear_pos.keys():
                search_idx[key] = 0

        find_idx = idx_new

    print(find_idx+loop_count*len(t)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

    appear_pos = {}  # 文字が出てくる場所
    for idx, T in enumerate(t):
        if T not in appear_pos:
            appear_pos[T] = [idx]
        else:
            appear_pos[T].append(idx)

    search_idx = {}
    for key in appear_pos.keys():
        search_idx[key] = 0

    loop_count = 0
    find_idx = 0
    for S in s:
        if S not in appear_pos:
            print(-1)
            sys.exit()

        if len(appear_pos[S]) > search_idx[S]:
            idx_new = appear_pos[S][search_idx[S]]
            search_idx[S] += 1
        else:
            # 一周している時
            loop_count += 1
            for key in appear_pos.keys():
                search_idx[key] = 0       
            find_idx = appear_pos[S][0]
            search_idx[key] = 1
            continue

        if idx_new <= find_idx:
            # これも一周している時
            loop_count += 1
            for key in appear_pos.keys():
                search_idx[key] = 0

        find_idx = idx_new

    print(find_idx+loop_count*len(t)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

    appear_pos = {}  # 文字が出てくる場所
    for idx, T in enumerate(t):
        if T not in appear_pos:
            appear_pos[T] = [idx]
        else:
            appear_pos[T].append(idx)

    search_idx = {}
    for key in appear_pos.keys():
        search_idx[key] = 0

    loop_count = 0
    find_idx = 0
    for S in s:
        if S not in appear_pos:
            print(-1)
            sys.exit()

        if len(appear_pos[S]) > search_idx[S]:
            idx_new = appear_pos[S][search_idx[S]]
        else:
            idx_new = appear_pos[S][0]
        search_idx[S] += 1

        if idx_new > find_idx:
            find_idx = idx_new
            continue

        # 初期化
        find_idx = idx_new
        loop_count += 1
        for key in appear_pos.keys():
            search_idx[key] = 0
        search_idx[S] += 1

    print(find_idx+loop_count*len(t)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

    appear_pos = {}  # 文字が出てくる場所
    for idx, T in enumerate(t):
        if T not in appear_pos:
            appear_pos[T] = [idx]
        else:
            appear_pos[T].append(idx)

    search_idx = {}
    for key in appear_pos.keys():
        search_idx[key] = 0

    loop_count = 0
    find_idx = 0
    for S in s:
        if S not in appear_pos:
            print(-1)
            sys.exit()

        if len(appear_pos[S]) > search_idx[S]:
            idx_new = appear_pos[S][search_idx[S]]
        else:
            idx_new = appear_pos[S][0]
        search_idx[S] += 1

        if idx_new >= find_idx:
            find_idx = idx_new
            continue

        # 初期化
        find_idx = idx_new
        loop_count += 1
        for key in appear_pos.keys():
            search_idx[key] = 0

    print(find_idx+loop_count*len(t)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

    appear_pos = {}  # 文字が出てくる場所
    for idx, T in enumerate(t):
        if T not in appear_pos:
            appear_pos[T] = [idx]
        else:
            appear_pos[T].append(idx)

    search_idx = {}
    for key in appear_pos.keys():
        search_idx[key] = 0

    loop_count = 0
    find_idx = 0
    for S in s:
        if S not in appear_pos:
            print(-1)
            sys.exit()

        if len(appear_pos[S]) > search_idx[S]:
            idx_new = appear_pos[S][search_idx[S]]
        else:
            idx_new = appear_pos[S][0]
        search_idx[S] += 1

        if idx_new > find_idx:
            find_idx = idx_new
            continue

        # 初期化
        find_idx = idx_new
        loop_count += 1
        for key in appear_pos.keys():
            search_idx[key] = 0

    print(find_idx+loop_count*len(t)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

s = input()
n = len(s)
s = s + s
t = input()
m = len(t)

ls = [[] for _ in range(26)]

for i in range(2*n):
    ls[ord(s[i])-97].append(i)

res = 0
flg = True
ind = 0
for i in range(m):
    if ls[ord(t[i])-97] == []:
        flg = False
        break
    nind = ls[ord(t[i])-97][bisect_left(ls[ord(t[i])-97],ind)]
    res += nind - ind 
    if nind >= n:
        nind -= n
    ind = nind
if flg:print(res+1)
else:print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect

    s = input()
    t = input()
    n = len(s)
    m = len(t)
    alpha = [[] for _ in range(26)]

    # KeyInsight
    # 部分列判定：場所i以降で文字Cが最初に現れる位置を高速に求める
    # アルファベットの出現位置をリストで記録

    # sで各文字が出現する位置を2周分記録
    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index)

    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index + n)

    ans = 1
    p = 0

    for ti in t:
        c = ord(ti) - ord('a')

        if len(alpha[c]) == 0:
            print('-1')
            exit()

        p = alpha[c][bisect(alpha[c], p)]

        if p >= n:
            p -= n
            ans += n

    ans += p

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect_left

    s = input()
    t = input()
    n = len(s)
    m = len(t)
    alpha = [[] for _ in range(26)]

    # KeyInsight
    # 部分列判定：場所i以降で文字Cが最初に現れる位置を高速に求める
    # アルファベットの出現位置をリストで記録

    # sで各文字が出現する位置を2周分記録
    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index)

    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index + n)

    ans = 1
    p = 0

    for ti in t:
        c = ord(ti) - ord('a')

        if len(alpha[c]) == 0:
            print('-1')
            exit()

        p = alpha[c][bisect_left(alpha[c], p)]

        if p >= n:
            p -= n
            ans += n

    ans += p

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect_left

    s = input()
    t = input()
    n = len(s)
    m = len(t)
    alpha = [[] for _ in range(26)]

    # KeyInsight
    # 部分列判定：場所i以降で文字Cが最初に現れる位置を高速に求める
    # アルファベットの出現位置をリストで記録

    # sで各文字が出現する位置を2周分記録
    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index)

    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index + n)

    ans = 1
    p = 0

    for ti in t:
        c = ord(ti) - ord('a')

        if len(alpha[c]) == 0:
            print('-1')
            exit()

        p = alpha[c][bisect_left(alpha[c], p + 1)]

        if p >= n:
            p -= n
            ans += n

    ans += p

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect_right

    s = input()
    t = input()
    n = len(s)
    m = len(t)
    alpha = [[] for _ in range(26)]

    # KeyInsight
    # 部分列判定：場所i以降で文字Cが最初に現れる位置を高速に求める
    # アルファベットの出現位置をリストで記録

    # sで各文字が出現する位置を2周分記録
    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index)

    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index + n)

    ans = 1
    p = 0

    for ti in t:
        c = ord(ti) - ord('a')

        if len(alpha[c]) == 0:
            print('-1')
            exit()

        p = alpha[c][bisect_right(alpha[c], p)]

        if p >= n:
            p -= n
            ans += n

    ans += p

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
s = list(map(str,input()))
t = list(map(str,input()))

if len(set(s) & set(t)) < len(set(t)):
    print(-1)
    sys.exit()

w = t[::-1]
q = [len(s)]

while len(w) > 0:
    a = w.pop()
    q.append(s.index(a)+1)

ans = q[1]
for i in range(2, len(t)+1):
    dif = q[i] - q[i-1]
    if dif > 0:
        ans += dif
    else:
        ans += q[0]+dif
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]:[] for i in range(len(S)-1)}
for i in range(len(S)-1):
  E[S[i]].append(i)
T = input()
print(E)
a = len(S)-1
ans=0
now = -1
for i in range(len(T)-1):
  if not T[i] in E.keys():
    print(-1)
    exit()
  s=E[T[i]]
  b= bisect.bisect_right(s,now)
  #print(s,b,now)
  if b==len(s):
    ans+=s[0]+1+a-1-now
    now=s[0]
  else:
    ans+=s[b]-now
    now=s[b]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]:[] for i in range(len(S)-1)}
for i in range(len(S)-1):
  E[S[i]].append(i)
T = input()
#print(E)
a = len(S)-1
ans=0
now = 0
for i in range(len(T)-1):
  if not T[i] in E.keys():
    print(-1)
    exit()
  s=E[T[i]]
  b= bisect.bisect_right(s,now)
  #print(s,b,now)
  if b==len(s):
    ans+=s[0]+1+a-1-now
    now=s[0]
  else:
    ans+=s[b]-now
    now=s[b]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]:[] for i in range(len(S)-1)}
for i in range(len(S)-1):
  E[S[i]].append(i)
T = input()
#print(E)
a = len(S)-1
ans=0
now = 0
for i in range(len(T)):
  if not T[i] in E.keys():
    print(-1)
    exit()
  s=E[T[i]]
  b= bisect.bisect_right(s,now)
  #print(s,b,now)
  if b==len(s):
    ans+=s[0]+1+a-1-now
    now=s[0]
  else:
    ans+=s[b]-now
    now=s[b]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]:[] for i in range(len(S)-1)}
for i in range(len(S)-1):
  E[S[i]].append(i)
T = input()
#print(E)
a = len(S)-1
ans=0
now = 0
for i in range(len(T)):
  if not T[i] in E.keys():
    print(-1)
    exit()
  s=E[T[i]]
  b= bisect.bisect_right(s,now)
  #print(s,b,now)
  if b==len(s):
    ans+=s[0]+1+a-1-now
    now=s[0]
  else:
    ans+=s[b]-now
    now=s[b]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys,os,math
from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import Queue


# sys.stdin = open('input')
# n, k = map(int, raw_input().split())
# da = map(int, raw_input().split())
# db = map(int, raw_input().split())


def main():
    s = raw_input().strip()
    t = raw_input().strip()
    d = defaultdict(list)
    for idx, c in enumerate(s):
        d[c].append(idx)
    i, offset = 1, 0
    for c in t:
        idx = bisect.bisect_right(d[c], offset)
        if not d[c]:
            print -1
            return
        if idx == len(d[c]):
            i+=1
            offset = d[c][0]
        else:
            offset = d[c][idx]
    print (i-1)*len(s)+offset+1






main()

Submission status: Wrong Answer

Solution code:
import sys,os,math
from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import Queue


# sys.stdin = open('input')
# n, k = map(int, raw_input().split())
# da = map(int, raw_input().split())
# db = map(int, raw_input().split())


def main():
    s = raw_input().strip()
    t = raw_input().strip()

    d = defaultdict(list)
    ls = len(s)
    for idx, c in enumerate(s):
        d[c].append(idx)
    ans = 0
    i, offset = 1, 0
    for c in t:
        idx = bisect.bisect_left(d[c], offset)
        if not d[c]:
            print -1
            return
        if idx == len(d[c]):
            i+=1
            offset = d[c][0]
        else:
            offset = d[c][idx]
    # print i,offset
    print (i-1)*ls+offset+1


main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect


S = input()
T = input()

if set(T) - set(S):
    print(-1)
    exit()

dic = defaultdict(list)
leng_dic = defaultdict(int)

for pos, s in enumerate(S):
    dic[s].append(pos)
    leng_dic[s] += 1

length = len(S)
now = 0
count = 0
for t in T:
    if dic[t][-1] < now:
        now = 0
        count += 1
    idx = bisect.bisect_left(dic[t], now)
    now = dic[t][idx]

ans = count*length + now + 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect


S = input()
T = input()

if set(T) - set(S):
    print(-1)
    exit()

dic = defaultdict(list)
leng_dic = defaultdict(int)

for pos, s in enumerate(S):
    dic[s].append(pos)
    leng_dic[s] += 1

length = len(S)
now = -1
count = 0
for t in T:
    idx = bisect.bisect_left(dic[t], now)
    if idx == leng_dic[t]:
        now = -1
        count += 1
        idx = bisect.bisect_right(dic[t], now)
    now = dic[t][idx]

ans = count*length + now + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

s,t=input(),input()
ls=len(s)
lt=len(t)
A=[[] for i in range(26+1)]
L=[0 for i in range(26+1)]

for i in range(ls):
  A[ord(s[i])-96].append(i+1)
  L[ord(s[i])-96]+=1
#print(A)

tmp=0
ans=0
for i in range(lt):
  ii=ord(t[i])-96
  if L[ii]==0:
    print(-1)
    exit()
  else:
    #print(A[ii],tmp)
    iii=bisect.bisect_left(A[ii],tmp)
    if iii==L[ii]:
      ans+=ls-tmp+A[ii][0]
      tmp=A[ii][0]
    else:
      ans+=A[ii][iii]-tmp
      tmp=A[ii][iii]
print(ans)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    import collections
    import bisect
    input = sys.stdin.readline
    s = str(input().rstrip('\n'))
    t = str(input().rstrip('\n'))
    d = collections.defaultdict(list)
    for i in range(len(s)):
        d[s[i]] += [i]

    loop = 0
    pos = 0
    for i in range(len(t)):
        if t[i] not in d:
            print(-1)
            exit()
        else:
            pos = bisect.bisect_left(d[t[i]], pos)
            if len(d[t[i]]) == pos:
                loop += 1
                pos = 0
                pos = d[t[i]][0]
            else:
                pos = d[t[i]][pos] + 1
    print(loop * len(s) + pos)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import bisect

def main():
    s = input()
    t = input()
    n = len(s)
    m = len(t)

    l = [[] for _ in range(26)]
    for i in range(n):
        l[ord(s[i]) - ord('a')].append(i)
        l[ord(s[i]) - ord('a')].append(i+n)

    ans = 0
    p = 0
    for i in range(m):
        c = ord(t[i]) - ord('a')
        if len(l[c]) == 0:
            print(-1)
            return
        p = l[c][bisect.bisect_left(l[c], p)] + 1
        if p >= n:
            p -= n
            ans += n
    ans += p
    print(ans)
    return

main()

Submission status: Wrong Answer

Solution code:
import bisect
ai = [[] for i in range(26)]
s = list(input())
t = list(input())
n = len(s)
for i in range(n):
    ai[int(ord(s[i])-97)].append(i)
n2 = len(t)
count = 0
if len(ai[int(ord(t[0]))-97]) > 0:
    now = ai[int(ord(t[0]))-97][0]
    for i in range(1,n2):
        nex = int(ord(t[i]))-97
        n3 = len(ai[nex])
        if n3 == 0:
            count = 0
            now = 0
            break
        else:
            ni = bisect.bisect_right(ai[nex],now)
            #print(ni,count,now)
            if ni == n3:
                ni = 0
            if ai[nex][ni] > now:
                now = ai[nex][ni]
            else:
                count += 1
                now = ai[nex][ni]
    print(count*n+now+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    t = list(reversed(list(input())))

    for T in t:
        if T not in s:
            print(-1)
            return

    for loop in range(100):
        for idx in range(len(s)):
            if s[idx] == t[-1]:
                t.pop()
                if len(t) == 0:
                    print(idx+1+loop*len(s))
                    return

    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

#dict型 各アルファベットのインデックスを登録
alp = {} #{str:list[int,int,~~],:,:}
for i,s in enumerate(S):
    alp.setdefault(s,[]).append(i)
# print(alp)


bef_idx = -1
ans = 0
for t in T:
    # print(t)
    if t not in alp:
        ans -= (bef_idx+2)
        break

    idx_list = alp[t]
    #bef_idxより右にtが無ければ次の連結
    #bef_idxは一番最後に足す
    if idx_list[-1] <= bef_idx:
        ans += len(S)
        bef_idx = -1
    idx = bisect.bisect_left(idx_list,bef_idx+1)
    # print(bef_idx,idx)
    bef_idx = idx_list[idx]
    # print(ans)
ans += (bef_idx+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

#dict型 各アルファベットのインデックスを登録
alp = {} #{str:list[int,int,~~],:,:}
for i,s in enumerate(S):
    alp.setdefault(s,[]).append(i)
# print(alp)


bef_idx = -1
ans = 0
for t in T:
    # print(t)
    if t not in alp:
        ans -= (bef_idx+2)
        break

    idx_list = alp[t]
    #bef_idxより右にtが無ければ次の連結
    #bef_idxは一番最後に足す
    if idx_list[-1] <= bef_idx:
        ans += len(S)
        bef_idx = -1
    idx = bisect.bisect_right(idx_list,bef_idx)
    # print(bef_idx,idx)
    bef_idx = idx_list[idx]
    # print(ans)
ans += (bef_idx+1)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
T = input()

#dict型 各アルファベットのインデックスを登録
alp = {} #{str:list[int,int,~~],:,:}
for i,s in enumerate(S):
    alp.setdefault(s,[]).append(i)
# print(alp)


bef_idx = -1
ans = 0
for t in T:
    # print(t)
    if t not in alp:
        ans -= (bef_idx+2)
        break

    idx_list = alp[t]
    #bef_idxより右にtが無ければ次の連結
    if max(idx_list) < bef_idx:
        ans += len(S)
        bef_idx = -1
    for idx in idx_list:#bisectの方が良い
        if bef_idx < idx:
            # print(bef_idx,idx)
            bef_idx = idx
            break
    # print(ans)
ans += (bef_idx+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    S=input()
    T=input()
    if(set(T)-set(S)):
        print(-1)
        return
    n=len(S)
    C=[[] for _ in range(26)]
    for i,s in enumerate(S*2): C[ord(s)-97].append(i)
    from bisect import bisect_left
    ans=0
    for s in T:
        j=bisect_left(C[ord(s)-97],(ans+1)%n)
        ans+=C[ord(s)-97][j]-ans%n
    print(ans+1)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    S=input()
    T=input()
    if(set(T)-set(S)):
        print(-1)
        return
    n=len(S)
    C=[[] for _ in range(26)]
    for i,s in enumerate(S*2): C[ord(s)-97].append(i)
    from bisect import bisect_left
    ans=0
    for s in T:
        j=bisect_left(C[ord(s)-97],(ans+1)%n)
        ans+=C[ord(s)-97][j]-ans%n+1
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    S=input()
    T=input()
    if(set(T)-set(S)):
        print(-1)
        return
    n=len(S)
    C=[[] for _ in range(26)]
    for i,s in enumerate(S*2): C[ord(s)-97].append(i)
    from bisect import bisect_left
    ans=0
    for s in T:
        j=bisect_left(C[ord(s)-97],(ans+1)%n)
        ans+=(C[ord(s)-97][j]-(ans+1)%n)+1
    print(ans+1)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    S=input()
    T=input()
    if(set(T)-set(S)):
        print(-1)
        return
    n=len(S)
    C=[[] for _ in range(26)]
    for i,s in enumerate(S*2): C[ord(s)-97].append(i)
    from bisect import bisect_left
    ans=0
    for s in T:
        j=bisect_left(C[ord(s)-97],(ans+1)%n)
        ans+=(C[ord(s)-97][j]-(ans%n))
    print(ans+1)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

s = str(input())
t = str(input())

if set(t).issubset(set(s)):
    p = -1
    r = 0
    
    for i in t:
        p = s.find(i, p+1)
        if p == -1:
            r += 1
            p = s.find(i)
        
    print(p + 1 + len(s)*r)
    
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import string

s = input()
t = input()

doubles = s + s

nextpos = [[-1 for i in range(30)] for j in range(len(s) + 1)]
alphas = list(string.ascii_lowercase)

for alpidx in range(len(alphas)):
	alp = alphas[alpidx]
	lastpos = -1
	for idx in range(len(doubles) - 1, -1, -1):
		if(idx < len(s)):
			nextpos[idx][alpidx] = lastpos - idx
		if(doubles[idx] == alp):
			lastpos = idx
		
pos = 0
for ch in t:
	index = ord(ch) - 97
	mod = pos%len(s)
	if(nextpos[mod][index] < 0):
		print(-1)
		exit(0)
	distance = nextpos[mod][index]
	pos += distance
print(pos+1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()

if t == s:
    ans = len(t)
else:
    try:
        ans = s.index(t[0]) + 1
        for i in range(1, len(t)):
            if t[i] in s:
                if s.index(t[i]) >= s.index(t[i-1]):
                    ans += s.index(t[i]) - s.index(t[i-1])
                else:
                    ans += s.index(t[i]) - s.index(t[i-1]) + len(s)
            else:
                ans = -1
                break
    except ValueError:
        ans = -1
                
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()

if t == s:
    ans = len(t)
else:
    try:
        ans = s.index(t[0]) + 1
        for i in range(1, len(t)):
            if t[i] in s:
                if s.index(t[i]) >= s.index(t[i-1]):
                    ans += s.index(t[i]) - s.index(t[i-1])
                else:
                    ans += s.index(t[i]) - s.index(t[i-1]) + len(s)
    except ValueError:
        ans = -1
                
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

Sset = set(list(S))
Tset = set(list(T))
if len(Tset-Sset) > 0:
  print(-1)
  exit()

SS = S * 2
SSpos = {}
for i in range(len(SS)):
  if SS[i] in SSpos:
    SSpos[SS[i]].append(i)
  else:
    SSpos[SS[i]] = [i]
#print(SSpos)

ans = 1
i = 0
for t in T:
  j = bisect.bisect_left(SSpos[t],i)
  ans += SSpos[t][j] - i
  #print(t,SSpos[t],ans,i,j)
  i = SSpos[t][j]
  if i >= len(S):
    i -= len(S)
print(ans)