알고리즘 문제풀이

Send me the money(백준 15786)

wiojfe 2023. 9. 14. 01:13

 순서대로 문자를 찾아내야 하는데 만약 찾는 문자가 없는 경우의 예외 처리를 잘 해줘야 한다. 브론즈 문제 이지만 좀 시간이 걸렸다. ㅠㅠ

import sys 
input = sys.stdin.readline 
n,m = map(int ,input().split())
tofind = input().rstrip()

def check(tofind, k) :
    idx = 0 
    total = 0 
    for i in range(len(tofind)) :
        tofindchar=tofind[i]
        j = idx 
        while True :
            if j>= len(k) :
                return "false"
                
            if k[j] == tofindchar :
                total += 1 
                idx +=1 
                # print(f"found {k[j]}")
                break 
            else :
                j += 1 
                idx = j 
        
    return "true" if total == len(tofind) else "false"   
  
        

for i in range(m):
    k  = input().rstrip()
    if len(k) <len(tofind):
        print("false")
    else :    
        print(check(tofind, k))