Problem:
 Max Score: 250 Points   Problem Statement Mr.X, who the handle name is T , looked at the list which written N handle names, S_1, S_2, ..., S_N . But he couldn't see some parts of the list. Invisible part is denoted ? .  Please calculate all possible index of the handle name of Mr.X when you sort N+1 handle names ( S_1, S_2, ..., S_N and T ) in lexicographical order. Note: If there are pair of people with same handle name, either one may come first.       Input The input is given from standard input in the following format.   N  S_1  S_2 : S_N  T       Output   Calculate the possible index and print in sorted order. The output should be separated with a space. Don't print a space after last number.  Put a line break in the end.     Constraints   1 ≤ N ≤ 10000  1 ≤ |S_i|, |T| ≤ 20 ( |A| is the length of A .)  S_i consists from lower-case alphabet and ? .  T consists from lower-case alphabet.     Scoring  Subtask 1 [ 130 points ]   There are no ? 's.   Subtask 2 [ 120 points ]   There are no additional constraints.        Sample Input 1  2
tourist
petr
e      Sample Output 1  1     There are no invisible part, so there are only one possibility.
					The sorted handle names are e , petr , tourist , so e comes first.     Sample Input 2  2
?o?r?s?
?et?
e

Solution (Python):

import sys
readline = sys.stdin.readline

N = int(readline())

Alist = [None] * N
Zlist = [None] * N
for i in range(N):
  line = readline().rstrip()
  Alist[i] = line.replace("?","a")
  Zlist[i] = line.replace("?","z")
  
Alist = sorted(Alist)
Zlist = sorted(Zlist)

T = readline().rstrip()

import bisect
end = bisect.bisect_right(Alist, T)
start = bisect.bisect_left(Zlist, T)

print(*list(range(start + 1, end + 2)))

Solution (Python):
from bisect import*
n,*s,t=open(0)
x,y=[sorted(u.replace('?',c)for u in s)for c in'za']
print(*range(bisect_left(x,t)+1,bisect(y,t)+2))

Solution (Python):
from bisect import*
n,*s,t=open(0)
print(*range(bisect_left(sorted(u.replace(*'?z')for u in s),t)+1,bisect(sorted(u.replace(*'?a')for u in s),t)+2))

Solution (Python):
from bisect import*
n,*s,t=open(0)
print(*range(bisect_left(sorted(u.replace(*'?z')for u in s),t)+1,bisect(sorted(u.replace(*'?a')for u in s),t)+2))

Solution (Python):
import bisect
N = int(input())
S = [input() for _ in range(N)]
T = input() 
best = []
worse = []
for s in S:
    best.append(s.replace('?', 'z'))
    worse.append(s.replace('?', 'a'))
ans=[]
best.sort()
worse.sort()
for i in range(bisect.bisect_left(best,T)+1,bisect.bisect_right(worse,T)+2):
    ans.append(i)
print(*ans)

Solution (Python):
def main():
    N = int(input())
    S = [input() for _ in range(N)]
    t = input()
    mn = 1
    mx = N+1
    for s in S:
        smin = s.replace("?", "a")
        smax = s.replace("?", "z")
        if t < smin:
            mx -= 1
        if smax < t:
            mn += 1
    ans = list(range(mn, mx+1))
    print(*ans)


if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import functools 
import sys
import bisect
import string
import math
import time
import random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'

    mp=[1]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

N=10**6+2
m=[0]*(N)

n=I()
a=[]
for i in range(n):
    a+=[input()]
t=input()  
L,F=0,0
for i in a:
    f=''
    l=''
    for j in i:
        if j=='?':
            f+='a'
            l+='z'
        else:
            f+=j
            l+=j
    
    if f<=t:
        F+=1
    if l<t:
        L+=1
        
print(*list(range(L+1,F+1+1)))

Solution (Python):
n = int(input())
l = [input() for _ in range(n)]
s = input()

def small(l, s):
    p = [s > i.replace('?', 'z') for i in l]
    return sum(p)

def big(l, s):
    p = [s >= i.replace('?', 'a') for i in l]
    return sum(p)

print(*list(range(small(l, s) + 1, big(l, s) + 2)))

Solution (Python):
import bisect
N = int(input())
S = [input() for _ in range(N)]
T = input()

S1 = []
S2 = []
for s in S:
    S1.append(s.replace('?', 'z'))
    S2.append(s.replace('?', 'a'))
S1 = sorted(S1)
S2 = sorted(S2)
ans1 = bisect.bisect_left(S1, T)+1
ans2 = bisect.bisect_right(S2, T)+1


print(*range(ans1, ans2+1))

Solution (Python):
n = int(input())
l = [input() for _ in range(n)]
t = input(); l.append(t)
li = list(map(lambda x:x.replace('?', 'A'), l))
ho = list(map(lambda x:x.replace('?', 'z'), l))
li.sort(reverse=True); ho.sort()
x = n - li.index(t); y = ho.index(t)
print(' '.join(map(str, list(range(y+1,x+2)))))

Solution (Python):
n=int(input())
la=[]
lz=[]
for i in range(n):
    s=list(input())
    sa=''
    sz=''
    for j in s:
        if j=='?':
            sa+='a'
            sz+='z'
        else:
            sa+=j
            sz+=j
    la.append(sa)
    lz.append(sz)
t=input()
p=1
q=1
for i in la:
    if t>=i:
        p+=1
for i in lz:
    if t>i:
        q+=1
ans=[i for i in range(q,p+1)]
print(*ans)

Solution (Python):
n=int(input())
s = [input()for _ in range(n)]
t = input()
s += [t]
a = s.copy()
z = s.copy()
for i in range(n):
  a[i] = a[i].replace('?', 'a')
  z[i] = z[i].replace('?', 'z')
a.sort(reverse=True)
z.sort()
idx1 = z.index(t)
idx2 = n - a.index(t)
print(' '.join(map(str,range(idx1 + 1, idx2 + 2))))

Solution (Python):
def list_rindex(li, x):
    for i in reversed(range(len(li))):
        if li[i] == x:
            return i
    raise ValueError("{} is not in list".format(x))

N = int(input())
S = [input() for _ in range(N)]
T = input()

aS = [s.replace("?", "a") for s in S] + [T]  
zS = [s.replace("?", "z") for s in S] + [T]

aS.sort()
zS.sort()

asf = list_rindex(aS, T)
zsf = zS.index(T)

ans = [i for i in range(zsf + 1, asf + 2)]
print(*ans)

Solution (Python):
n=int(input())
c,d=1,n+1
s=[input()for i in range(n)]
t=input()
for i in range(n):
    if s[i]!=t:
        p,q=s[i].replace("?","a"),s[i].replace("?","z")
        if p>t:
            d-=1
        elif q<t:
            c+=1
print(*[i for i in range(c,d+1)])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())

S = [input().rstrip() for _ in range(N)]
T = input().rstrip()

first = 1
last = 1
for s in S:
    if s.replace('?','z') < T:
        first += 1
    if s.replace('?','a') <= T:
        last += 1

print(' '.join(str(x) for x in range(first,last+1)))

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,random
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

\

N = int(input())
SS = [input() for _ in range(N)]
T = input()
before = 0
after = 0

for S in SS:
    sS = S.replace('?','a')
    lS = S.replace('?','z')
    if T < sS:
        after += 1
    elif lS < T:
        before += 1

print (' '.join(map(str,list(range(before+1,N-after+2)))))

Solution (Python):


import sys
from bisect import bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
L1=['']*N
L2=['']*N
for i in range(N):
    S=input()
    L1[i]=S.replace('?', 'a')
    L2[i]=S.replace('?', 'z')
L1.sort()
L2.sort()
T=input()

idx1=bisect_left(L2, T)
idx2=bisect_right(L1, T)
print(*list(range(idx1+1, idx2+2)))

Solution (Python):
n=int(input());s=[input()for i in range(n)];b=input();ans=[]
def change(s,az):
  ans=""
  for i in s:ans+=i if i!="?"else az
  return ans
a,z=[b],[b]
for i in s:
  a.append(change(i,"a"))
  z.append(change(i,"z"))
a.sort();z.sort()
for i in range(n+1):
  if a[i]==b:ans.append(i+1);break
for i in range(1,n+2):
  if z[-i]==b:ans.append(n+2-i);break
ans.sort();anss=str(ans[0])
for i in range(1,ans[1]-ans[0]+1):anss+=" "+str(ans[0]+i)
print(anss)

Solution (Python):
n = int(input())
a,z = [],[]
for _ in range(n):
	s = input()
	a.append(s.replace("?","a"))
	z.append(s.replace("?","z"))
t = input()
a.append(t)
z.append(t)
a,z = sorted(a)[::-1],sorted(z)
r,l = n+1-a.index(t),z.index(t)+1
for i in range(l,r): print(i,end=" ")
print(r)

Solution (Python):
def iin(): return int(input())
def nl(): return list(map(int, input().split()))

n = iin()
ss = [input() for _ in range(n)]
t = input()

i_min = n + 1
i_max = 1
for s in ss:
    if t >= s.replace('?', 'a'):
        i_max += 1
    if t <= s.replace('?', 'z'):
        i_min -= 1
ans = range(i_min, i_max + 1)
print(*ans)

Solution (Python):
N = int(input())
S = [input().strip() for i in range(N)]
T = input().strip()

lower = 1  
upper = N + 1  
for s in S:
    
    
    s_first = s.replace('?', 'a')
    s_last = s.replace('?', 'z')
    if T < s_first:
        upper -= 1  
    if s_last < T:
        lower += 1  

ans = ' '.join(map(str, range(lower, upper + 1)))
print(ans)

Solution (Python):
N=int(input())
S=[input() for i in range(N)]
T=input()
L=1
H=N+1
for i in range(N):
    A=S[i].replace("?","a")
    Z=S[i].replace("?","z")
    if T<A:
        H-=1
    if Z<T:
        L+=1
st=""
for i in range(L,H+1):
    st+=str(i)
    if i==H:
        continue
    st+=" "
print(st)

Solution (Python):
def main():
    N = int(input())
    S = [input() for i in range(N)]
    T = input()

    S1 = [s.replace("?", "z") for s in S]
    S2 = [s.replace("?", "a") for s in S]
    S1.append(T)
    S2.append(T)

    idx1 = sorted(S1).index(T)
    idx2 = (N+1) - 1 - sorted(S2, reverse=True).index(T)

    ans = range(idx1+1, idx2+1+1)
    print(*ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())

s = []
for i in range(n):
	s.append(input())
t = input()

before = 0
after = 0

for i in s:
	if t < i.replace('?','a'):
		after += 1
	if i.replace('?','z') < t:
		before += 1


print(*range(before+1,n-after+2))

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]
T = input()

pre = 0
post = 0
for s in S:
	if s.replace('?','z') < T:
		pre += 1
	elif s.replace('?','a') > T:
		post += 1

print(*range(pre + 1, N - post + 2))

Solution (Python):
N = int(input())
S = [(input(), 0) for i in range(N)]
t = (input(), 1)
S += t,
S.sort(key=lambda x: (x[0].replace(*'?z'), 1^x[1]))
l = S.index(t)+1
S.sort(key=lambda x: (x[0].replace(*'?a'), x[1]))
r = S.index(t)+1
print(*range(l, r+1))

Solution (Python):
N = int(input())
src = [input() for i in range(N)]
T = input()

before = after = 0
for s in src:
    if s.replace('?','z') < T:
        before += 1
    elif s.replace('?','a') > T:
        after += 1
print(*range(before+1, N-after+2))

Solution (Python):
n=int(input())
s=[input() for _ in range(n)]
t=input()
ls,fs=[],[]
for i in range(n):
    a = s[i].replace('?', 'a')
    z = s[i].replace('?', 'z')
    if a <= t <= z:
        fs.append(s[i])
    if z < t:
        ls.append(s[i])
l = len(ls)+1
r = l + len(fs)
print(' '.join(map(str,range(l, r+1))))

Solution (Python):
import sys
N = int(input())
S = [sys.stdin.readline().rstrip() for i in range(N)]
T = sys.stdin.readline().rstrip()
FS = [S[i].replace('?','z') for i in range(N)]
BS = [S[i].replace('?','a') for i in range(N)]
FS.append(T)
BS.append(T)
list.sort(FS)
list.sort(BS)
ends = len(BS) - BS[-1::-1].index(T)
for i in range(FS.index(T)+1,ends):
	print(i,end=" ")
print(ends)

Solution (Python):
n = int(input())
s_list = list()
for i in range(n):
    s_list.append(input())
t = input()

s_list_a = [s.replace("?","a") for s in s_list]
s_list_a.append(t)
s_list_a.sort()
index_a = [i+1 for i,x in enumerate(s_list_a) if x == t]

s_list_z = [s.replace("?","z") for s in s_list]
s_list_z.append(t)
s_list_z.sort()
index_z = [i+1 for i,x in enumerate(s_list_z) if x == t]

indexes = [str(i) for i in range(index_z[0],index_a[-1]+1)]

answer = ' '.join(indexes)

print(answer)

Solution (Python):
n = int(raw_input())
l = [raw_input().strip() for _ in xrange(n)]
t = raw_input().strip()
for i in xrange(n):
    l[i] += '0' * (20 - len(l[i]))
t += '0' * (20 - len(t))
a = b = p = 0
for s in l:
    c = 0
    for i in xrange(20):
        if s[i] != '?' and s[i] < t[i]:
            c |= 1
            break
        if s[i] != '?' and s[i] > t[i]:
            c |= 2
            break
        if s[i] == '0' and t[i] == '0':
            c |= 3
            break
        if s[i] == '0' and t[i] != '0':
            c |= 1
            break
        if s[i] != '0' and t[i] == '0':
            c |= 2
            break
        if s[i] == t[i]:
            continue
        if t[i] != 'a':
            c |= 1
        if t[i] != 'z':
            c |= 2
    else:
        c |= 3
    if c == 3:
        p += 1
    elif c == 1:
        a += 1
    elif c == 2:
        b += 1
r = range(a + 1, a + p + 2)
print ' '.join(map(str, r))