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:
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)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

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:
  if t not in SS:
    print(-1)
    exit()
  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)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

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:
  if t not in SS:
    print(-1)
    exit()
  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)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
data = {}
max = len(s)
for i in range(26):
    data[chr(i + ord('a'))] = max + 1
for i in range(len(s)):
    if data[s[i]] > i + 1:
        data[s[i]] = i + 1
ans = 0
pre = max
for i in range(len(t)):
    if data[t[i]] == max + 1:
        print(-1)
        exit()
    else:
        if pre < data[t[i]]:
            ans += (data[t[i]] - pre)
            pre = data[t[i]]
        else:
            ans += (max + data[t[i]] - pre)
            pre = data[t[i]]
    
print(ans)

Submission status: Wrong Answer

Solution code:

if __name__ == "__main__":
    s = input()
    alphabet = set(s)
    t = input()
    impossible = False
    for i in range(len(t)):
        if t[i] not in alphabet:
            print("-1")
            impossible = True
    if not impossible:
        i = 0
        found = 0
        for i in range(len(s) * (10**100)):
            if (found >= len(t)):
                break
            if s[i % len(s)] == t[found]:
                found += 1
        print(i)

Submission status: Wrong Answer

Solution code:

# import sys
# input = sys.stdin.readline
import bisect


def ch_to_int(c):
    return ord(c) - 96


def main():
    s = input()
    t = input()
    pos = 0
    count = 0
    ch = [[] for i in range(27)]

    for i in range(len(s)):
        ch[ch_to_int(s[i])].append(i)

    for i in range(len(t)):
        c = ch_to_int(t[i])
        li = ch[c]

        if len(li) == 0:
            pos = -2
            count = 0
            break

        next_index = bisect.bisect_left(li, pos)
        # print(next_index, pos, li)
        if next_index >= len(li):
            next_pos = li[0]
            count += 1
            pos = next_pos + 1
        else:
            if pos == li[next_index]:
                pos += 1
            else:
                pos = li[next_index] + 1
        # print(li, next_index, pos, count)

    print(pos + count * len(s))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

# import sys
# input = sys.stdin.readline
import bisect


def ch_to_int(c):
    return ord(c) - 96


def main():
    s = input()
    t = input()
    pos = 0
    count = 0
    ch = [[] for i in range(27)]

    for i in range(len(s)):
        ch[ch_to_int(s[i])].append(i)

    for i in range(len(t)):
        c = ch_to_int(t[i])
        li = ch[c]

        if len(li) == 0:
            pos = -2
            count = 0
            break

        next_index = bisect.bisect_left(li, pos)
        # print(next_index, pos, li)
        if next_index >= len(li):
            next_pos = li[0]
            count += 1
            pos = next_pos
        else:
            pos = li[next_index]
        # print(li, next_index, pos, count)

    print((pos + 1) + count * len(s))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect
import collections

s = input().rstrip()
t = input().rstrip()

counter_s = collections.Counter(s)
counter_t = collections.Counter(t)
for c in counter_t.keys():
    if c not in counter_s.keys():
        print(-1)
        exit()

s_indices = collections.defaultdict(list)
for i, c in enumerate(s):
    if c in counter_t.keys():
        s_indices[c].append(i)

index = 0
count = 0
offset = 0
while index < len(t):
    c = t[index]
    i = bisect.bisect_left(s_indices[c], offset)
    if i == len(s_indices[c]):
        count += 1
        offset = 0
    else:
        offset = s_indices[c][i]
        index += 1
print(len(s) * count + offset + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
from bisect import bisect_left
s = input()
t = input()

cmap = dd(list)
for i, c in enumerate(s):
    cmap[c].append(i)
cmap = dict(cmap)
a = 0
q = 0
for c in t:
    try:
        j = bisect_left(cmap[c], a % len(s))
    except:
        print(-1)
        exit()
    if j < len(cmap[c]):
        a = cmap[c][j]
    else:
        q += 1
        a = cmap[c][-1]

print(len(s)*q + a + 1)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 3500
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 2500
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 5000
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 2000
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 1000
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1

s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 500
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 300
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 600
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 100
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 10
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
def find(i, ss, step):
    for j in range(step, len(ss)):
        #print(i, j, ss[j])
        if i == ss[j]:
            a = j
            return a, step
    return -1


s = input().strip()
#print(s)
t = input().strip()
#print(t)
ss = s * 1000
#print(ss)
step = 0
for i in t:
    a = find(i, ss, step)
    if a == -1:
        print(-1)
        break
    else:
        #print(a)
        step = a[0] + 1
        #print(step)
if a != -1:
    print(step)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()

n = -1
ans = 0


for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += len(s)
      n = -1
    else:
      n = l[i]
    if j == len(t) - 1:
      if i == len(l):
        ans += l[0] + 1
      else:
        ans += l[i]+1
  else:
    ans = -1
    break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()

n = -1
ans = 0

for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += len(s)
      n = l[0]
    elif j == len(t) - 1:
      ans += l[i]+1
    else:
      n = l[i]
  else:
    ans = -1
    break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()

n = 0
ans = 0

for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += len(s)
      n = l[0]
    elif j == len(t) - 1:
      ans += l[i]+1
    else:
      n = l[i]
  else:
    ans = -1
    break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()
n = 0
ans = 0

for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += i
      n = l[0]
    elif j == len(t) - 1:
      ans += i
    else:
      n = l[i]
  else:
    ans = -1
    break

print(ans)

Submission status: Wrong Answer

Solution code:
#Aのなかでlより大きな最小の値を得る.Aはソート済み
def minif(A,l):
  for a in A:
    if a > l:return a
  return -1

s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
d = {}
for ch in a:
  d[ch] = []
for ch,n in zip(s,range(len(s))):
  d[ch].append(n)

ret = []
p = -1
for ch in t:
  if len(d[ch])==0:
    print("-1")
    exit()
  else:
    p = minif(d[ch],p)
    if p!=-1:ret.append(p)
    else:
      ret.append(d[ch][0])
      p = d[ch][0]

rep = 1
for i in range(1,len(t)):
  if ret[i] < ret[i-1]:rep+=1

e = len(s) - ret[-1]

print(len(s)*rep - e + 1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect

#Aのなかでlより大きな最小の値を得る.Aはソート済み
def minif(A,l):
  for a in A:
    if a > l:return a
  return -1

s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
d = {}
for ch in a:
  d[ch] = []
for ch,n in zip(s,range(len(s))):
  d[ch].append(n)

ret = []
p = -1
for ch in t:
  if len(d[ch])==0:
    print("-1")
    exit()
  else:
    p = bisect(d[ch],p)
    if p!=-1:ret.append(p)
    else:
      ret.append(d[ch][0])
      p = d[ch][0]

rep = 1
for i in range(1,len(t)):
  if ret[i] < ret[i-1]:rep+=1

e = len(s) - ret[-1]

print(len(s)*rep - e + 1)

Submission status: Wrong Answer

Solution code:

#Aのなかでlより大きな最小の値を得る.Aはソート済み
def minif(A,l):
  for a in A:
    if a > l:return a
  return -1

s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
d = {}
for ch in a:
  d[ch] = []
for ch,n in zip(s,range(len(s))):
  d[ch].append(n)

ret = []
p = -1
for ch in t:
  if len(d[ch])==0:
    print("-1")
    exit()
  else:
    p = minif(d[ch],p)
    if p!=-1:ret.append(p)
    else:
      ret.append(d[ch][0])
      p = 0

rep = 1
for i in range(1,len(t)):
  if ret[i] < ret[i-1]:rep+=1

e = len(s) - ret[-1]

print(len(s)*rep - e + 1)

Submission status: Wrong Answer

Solution code:
#Aのなかでlより大きな最小の値を得る.Aはソート済み
def minif(A,l):
  for a in A:
    if a > l:return a
  return -1

s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
d = {}
for ch in a:
  d[ch] = []
for ch,n in zip(s,range(len(s))):
  d[ch].append(n)

for k in d:
  d[k].sort()

ret = []
p = -1
for ch in t:
  if len(d[ch])==0:
    print("-1")
    exit()
  else:
    p = minif(d[ch],p)
    if p!=-1:ret.append(p)
    else:ret.append(d[ch][0])

rep = 1
for i in range(1,len(t)):
  if ret[i] < ret[i-1]:rep+=1

e = len(s) - ret[-1]

print(len(s)*rep - e + 1)

Submission status: Wrong Answer

Solution code:
#Aのなかでlより大きな最小の値を得る.Aはソート済み
def minif(A,l):
  for a in A:
    if a > l:return a
  return -1

s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
d = {}
for ch in a:
  d[ch] = []
for ch,n in zip(s,range(len(s))):
  d[ch].append(n)

for k in d:
  d[k].sort()

ret = []
p = -1
for ch in t:
  if len(d[ch])==0:
    print("-1")
    exit()
  else:
    b = minif(d[ch],p)
    if b!=-1:ret.append(b)
    else:ret.append(d[ch][0])

rep = 1
for i in range(1,len(t)):
  if ret[i] < ret[i-1]:rep+=1

e = len(s) - ret[-1]

print(len(s)*rep - e + 1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
s = input()
t = input()
d = defaultdict(list)
for i,e in enumerate(s):
    d[e].append(i+1)

p = defaultdict(int)

counter = 0
positions = [-1]
for e in t:
    if len(d[e]) == 0:
        print(-1)
        sys.exit()
i = 0
while i < len(t):
    e = t[i]
    if p[e] >= len(d[e]):
        p = defaultdict(int)
        counter += 1
    value = d[e][p[e]%len(d[e])]+len(s)*counter
    if value > positions[-1]:
        positions.append(value)
        p[e] += 1
        i += 1
    else:
        p = defaultdict(int)
        counter += 1
print(positions[-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
s = input()
t = input()
d = defaultdict(list)
for i,e in enumerate(s):
    d[e].append(i+1)

p = defaultdict(int)

counter = 0
positions = [-1]
for e in t:
    if len(d[e]) == 0:
        print(-1)
        sys.exit()
i = 0
while i < len(t):
    e = t[i]
    value = d[e][p[e]%len(d[e])]+len(s)*counter
    if value > positions[-1]:
        positions.append(value)
        p[e] += 1
        i += 1
    else:
        p = defaultdict(int)
        counter += 1
print(positions[-1])

Submission status: Wrong Answer

Solution code:
import collections
import bisect
s = input()
t = input()

dict_pos = collections.defaultdict(list) # dict of index 
for i, ch in enumerate(s, 1): 
    dict_pos[ch].append(i)

count_loop = 0
last_pos = -1
for ch in t:
    list_pos = dict_pos[ch]
    if len(list_pos) == 0:
        print(-1)
        quit()
    if list_pos[-1] <= last_pos:
        last_pos = list_pos[0]
        count_loop += 1
    else:
        last_pos = list_pos[bisect.bisect_left(list_pos, last_pos)]

print(count_loop * len(s) + last_pos)

Submission status: Wrong Answer

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

pos = [0]*len(t)
for i, c in enumerate(t):
    ind = [ii for ii, v in enumerate(s) if v == c]
    if not ind:
        print(-1)
        exit()
    pos[i] = ind

print(pos)

lens = len(s)
last = pos[0][0]
print(last)
res = last + 1
for vals in pos[1:]:
    for i, val in enumerate(vals):
        if last < val:
            res += val - last
            break
        else:
            if i==len(vals)-1:
                res += (lens - last - 1) + (val + 1)
            else:
                continue
        last = val

print(res)

Submission status: Wrong Answer

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

pos = [-1]*len(t)
for i, c in enumerate(t):
    try:
        pos[i] = s.index(c)
    except ValueError:
        print(-1)
        exit()

lens = len(s)
last = pos[0]
res = last + 1
for i, val in enumerate(pos[1:]):
    if last < val:
        res += val - last
    else:
        res += (lens - last - 1) + (val + 1)
    last = val

print(res)

Submission status: Wrong Answer

Solution code:
def main():
    s = str(input())
    t = str(input())
    if set(t) <= set(s):
        x = 0
        counter = 0
        for i in t:
            x = s.find(i, x)
            if x == -1:
                counter += 1
                x = s.find(i)
        print(x+1+counter*len(s))
    else:
        print(-1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = str(input())
    t = list(input())
    if set(t) <= set(s):
        t.append('7')
        n = len(s)
        result = 0
        point = 0
        i = t.pop(0)
        while i != '7':
            x = s.find(i, point)
            if x != -1:
                result += x-point
                point = x
                i = t.pop(0)
            else:
                result += n-point
                point = 0
        print(result+1)
    else:
        print(-1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    s = str(input())
    t = list(input())
    if set(t) <= set(s):
        t.append('7')
        n = len(s)
        result = 0
        point = 0
        counter = 0
        i = t.pop(0)
        while i != '7':
            if i in s[point:]:
                x = s.find(i, point)
                result += x
                point += x + 1
                i = t.pop(0)
            else:
                result += n-point
                point = 0
                counter += 1
        print(result)
    else:
        print(-1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    s = str(input())
    t = str(input())
    s += s
    n = len(s)-1
    m = len(t)
    
    print(s)
    result = 0
    point = 0
    counter = 0
    i = t[0]
    c = 1
    while c <= m:
        print(point, i, result)
        if i in s[point:]:
            x = s[point:].index(i)
            result += x + 1
            point += x + 1
            if c < m:
                i = t[c]
            c += 1
            counter = 0
        else:
            result += n + 1 - point
            point = 0
            counter += 1
            if counter >= 2:
                print(-1)
                sys.exit()
    print(result)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
ls = len(s)
lt = len(t)
D = {}
for i, item in enumerate(s):
    try:
        D[item].append(i)
    except:
        D[item] = [i]

for i in D.keys():
    # print(D[i][0])
    D[i].append(D[i][0] + ls)

# print(D)

import bisect

def e():
    ans = 0
    ind = 0
    for i in range(lt):
        try:
            L = D[t[i]]
            # print(L)
            bs = L[bisect.bisect_left(L, ind)]
            ans += bs - ind
            ind = bs % ls
        except:
            return -1
    return ans+1

print(e())

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    s = str(input())
    t = str(input())
    n = len(s)-1
    m = len(t)-1
    result = 0
    point = 0
    counter = 0
    i = t[0]
    c = 1
    while c <= m:
        if i in s[point:]:
            x = s[point:].index(i)
            result += x + 1
            point += x + 1
            i = t[c]
            c += 1
            counter = 0
        else:
            result += n + 2 - point
            point = 0
            counter += 1
            if counter >= 10:
                print(-1)
                sys.exit()
    print(result)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    s = str(input())
    t = str(input())
    n = len(s)-1
    m = len(t)-1
    result = 0
    point = 0
    counter = 0
    i = t[0]
    c = 1
    while c <= m:
        if i in s[point:]:
            x = s[point:].index(i)
            result += x + 1
            point += x + 1
            i = t[c]
            c += 1
            counter = 0
        else:
            result += n + 2 - point
            point = 0
            counter += 1
            if counter >= 2:
                print(-1)
                sys.exit()
    print(result)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect
s = input()
t = input()

N = len(s)

set_s = set(s)
set_t = set(t)

for c in set_t:
    if c not in set_s:
        print(-1)
        exit()

# それぞれの文字のインデックスを記録
idxs = defaultdict(list)
for i in range(N):
    idxs[s[i]].append(i)


current_idx = 0
total = 0
for c in t:
    next_idx = bisect.bisect(idxs[c], current_idx) # bisect(bisect_right)だと同じやつがある場合その右のidxになる

    if next_idx == len(idxs[c]):
        total += N
        current_idx = idxs[c][0]
    else:
        current_idx = idxs[c][next_idx]


print(total + current_idx + 1)
#print(total , current_idx , 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect
s = input()
t = input()

N = len(s)

set_s = set(s)
set_t = set(t)

for c in set_t:
    if c not in set_s:
        print(-1)
        exit()

# それぞれの文字のインデックスを記録
idxs = defaultdict(list)
for i in range(N):
    idxs[s[i]].append(i)


current_idx = 0
total = 0
for c in t:
    #print("aaa")
    next_idx = bisect.bisect_right(idxs[c], current_idx) # bisect(bisect_right)だと同じやつがある場合その右のidxになるので今回は都合が良い
    #print(c, idxs[c], "aaa", next_idx)
    if next_idx == len(idxs[c]):
        total += N
        current_idx = idxs[c][0]
    else:
        current_idx = idxs[c][next_idx]


print(total + current_idx + 1)
#print(total , current_idx , 1)

Submission status: Wrong Answer

Solution code:
def main():
    s = str(input())
    t = str(input())
    num = [[] for _ in range(26)]
    s_ = len(s)
    t_ = len(t)
    count = 0
    st = -1
    for i in range(s_):
        num[ord(s[i])-97].append(i)

    for i in range(t_):
        if len(num[ord(t[i])-97]) == 0:
            print(-1)
            exit()
        else:
            for j in num[ord(t[i])-97]:
                if st < j:
                    st = j
                    break
                else:
                    count += 1
                    st = min(num[ord(t[i])-97])
                    break
    print(count*s_+st+1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left as bl
from bisect import insort_right as ir
from collections import defaultdict
s = input()
t = input()
len_s = len(s)

index = defaultdict(list)
for i, c in enumerate(s):
    ir(index[c], i)

# print(index)

ans = 1
now = 0
for c in t:
    if not index[c]:
        print('-1')
        break
    idx = bl(index[c], now)
    # print(c, idx)
    if idx == len(index[c]):
        # print(c, index[c][0], len_s)
        ans += len_s - now + index[c][0]
        now = index[c][0]
    else:
        ans += index[c][idx] - now
        now = index[c][idx]
    # print('ans:',ans,' now:', now)
    # ans += index[c][bl(index[c],now)]
else:
    print(ans)

Submission status: Wrong Answer

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


# analysis on S
ch = [[] for _ in range(26)]
for i, c in enumerate(s):
    ascii = ord(c) - 97
    ch[ascii].append(i)

bi_left = bisect.bisect_left
now = -1
ans = 0
for i, t in enumerate(t):
    ascii = ord(t) - 97
    if len(ch[ascii]) == 0:
        print("-1")
        exit()
    check = bi_left(ch[ascii], now)

    if check == len(ch[ascii]):
        now = ch[ascii][0]
        ans += 1
    else:
        now = ch[ascii][check]

print(ans*len(s)+now+1)

Submission status: Wrong Answer

Solution code:
import bisect
s = raw_input()
t = raw_input()
p, e, n = 0, 1, len(s)
ss = set(s)
for tt in t:
    q = p % n
    if not tt in ss:
        e = 0
        break
    tmp = s[q:] + s + s[:q]
    idx = bisect.bisect(tmp, tt)
    p += idx
print((p+1)*e-1)

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
    cur = 2
    for i in range(len(t)):
        p = bisect.bisect_left(d[t[i]], cur)
        if p == 0 and len(d[t[i]]) == 0:
            print(-1)
            exit()
        elif p == len(d[t[i]]):
            loop += 1
            cur = d[t[i]][0] + 1
        else:
            cur = d[t[i]][p] + 1
    print(len(s) * loop + cur)


if __name__ == '__main__':
    slove()

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
    cur = 2
    for i in range(len(t)):
        p = bisect.bisect_left(d[t[i]], cur)
        if p == 0:
            print(-1)
            exit()
        elif p > len(d[t[i]]):
            loop += 1
            cur = d[t[i]][0] + 1
    print(len(s) * loop + cur)


if __name__ == '__main__':
    slove()

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
    cur = 2
    for i in range(len(t)):
        p = bisect.bisect_right(d[t[i]], cur)
        if p >= len(d[t[i]]):
            loop += 1
            if len(d[t[i]]) >= 1:
                cur = d[t[i]][0] + 1
            else:
                print(-1)
                exit()
        else:
            cur = d[t[i]][p] + 1
    print(len(s) * loop + cur)


if __name__ == '__main__':
    slove()

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
    cur = 2
    for i in range(len(t)):
        p = bisect.bisect_left(d[t[i]], cur)
        if p >= len(d[t[i]]):
            loop += 1
            if len(d[t[i]]) >= 1:
                cur = d[t[i]][0] + 1
            else:
                print(-1)
                exit()
        else:
            cur = d[t[i]][p] + 1
    print(len(s) * loop + cur)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
len_s = len(s)
t = input()

ind_dict = {i : [] for i in t}
for i, _s in enumerate(s):
    if _s in t:
        ind_dict[_s] += [i]

def get_least(last, s, dic):
    ls = dic[s]
    ind = bisect.bisect_right(ls, last)
    if ind >= len(ls):
        return ls[0]
    else:
        return ls[ind]

if -1 in [s.find(_t) for _t in t]:
    print(-1)
else:
    cnt = 0
    last = get_least(0, t[0], ind_dict)
    _next = last
    for _t in t[1:]:
        _next = get_least(last, _t, ind_dict)
        if _next <= last:
            cnt += 1
        last = _next
    print(cnt * len(s) + _next + 1)