Problem:


Search III

Your task is to write a program of a simple dictionary which implements the following instructions:

insert str: insert a string str in to the dictionary

find str: if the distionary contains str, then print 'yes', otherwise print 'no'

Input

In the first line n, the number of instructions is given. In the following n lines, n instructions are given in the above mentioned format.

Output

Print yes or no for each find instruction in a line.

Constraints

A string consists of 'A', 'C', 'G', or 'T'

1 ≤ length of a string ≤ 12

 n ≤ 1000000 

Sample Input 1

5
insert A
insert T
insert C
find G
find A

Sample Output 1

no
yes

Sample Input 2

13
insert AAA
insert AAC
insert AGA
insert AGG
insert TTT
find AAA
find CCC
find CCC
insert CCC
find CCC
insert T
find TTT
find T

Sample Output 2

yes
no
no
yes
yes
yes

Notes

Template in C



Sample input:
5
insert A
insert T
insert C
find G
find A


Sample output:
no
yes

Submission status: Time Limit Exceeded

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

class HashTable(list):
    def __init__(self, length):
        list.__init__(self)
        self.length = length
        self[:] = [None] * length

    def h1(self, key):
        return key % self.length

    def h2(self, key):
        return 1 + (key % (self.length - 1))

    def h(self, key, i):
        return (self.h1(key) + i*self.h2(key)) % self.length

    def insert(self, key):
        i = 0
        while True:
            j = self.h(key, i)
            if self[j] is None:
                self[j] = key
                return j
            else:
                i += 1
    
    def search(self, key):
        i = 0
        while True:
            j = self.h(key, i)
            if self[j] == key:
                return j
            elif self[j] is None or i >= self.length:
                return None
            else:
                i += 1

def getNum(char):
    char2num = str.maketrans("ACGT", "1234")
    return int(char.translate(char2num), 5)

if __name__ == '__main__':

    n = int(input())
    T = HashTable(1046527)
    C = [input().split(" ") for i in range(n)]
    
    for command, char in C:
        num = getNum(char)
        if command[0] == "i":
            T.insert(num)
        else:
            if T.search(num) is None:
                print("no")
            else:
                print("yes")

Submission status: Time Limit Exceeded

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

class HashTable(list):
    def __init__(self, length):
        list.__init__(self)
        self.length = length
        self[:] = [None] * length

    def h(self, key, i):
        h1 = key % self.length
        h2 = 1 + (key % (self.length - 1))
        return (h1 + i*h2) % self.length

    def insert(self, key):
        i = 0
        while True:
            #print(i)
            j = self.h(key, i)
            if self[j] is None:
                self[j] = key
                return j
            else:
                i += 1
    
    def search(self, key):
        i = 0
        while True:
            #print(i)
            j = self.h(key, i)
            if self[j] == key:
                return j
            elif self[j] is None or i >= self.length:
                return None
            else:
                i += 1

def getNum(char):
    char2num = str.maketrans("ACGT", "1234")
    return int(char.translate(char2num), 5)

if __name__ == '__main__':

    n = int(input())
    T = HashTable(1046527)
    C = [input().split(" ") for i in range(n)]
    
    for command, char in C:
        num = getNum(char)
        if command[0] == "i":
            T.insert(num)
        else:
            if T.search(num) is None:
                print("no")
            else:
                print("yes")

Submission status: Time Limit Exceeded

Solution code:
d=[]
for _ in[0]*int(input()):
 e=input().split()
 if'i'==e[0][0]:d+=[e[1]]
 else:print(['no','yes'][e[1]in d])

Submission status: Time Limit Exceeded

Solution code:
import sys
d=[]
input()
for e in sys.stdin:
 c,g=e.split()
 if'i'==c[0]:d+=[g]
 else:print(['no','yes'][g in d])

Submission status: Time Limit Exceeded

Solution code:
class Dictionary:
    def __init__(self):
        self.dic = []
    def insert(self, key):
        if not self.find(key):
            self.dic.append(key)
    def find(self, key):
        return key in self.dic

dic = Dictionary()
n = int(input())
for i in range(n):
    cmd = input().split()
    if cmd[0] == "insert":
        dic.insert(cmd[1])
    elif cmd[0] == "find":
        if dic.find(cmd[1]):
            print("yes")
        else:
            print("no")
    else:
        print("wrong command")

Submission status: Time Limit Exceeded

Solution code:
class Dictionary:
    def __init__(self):
        self.dic = []
    def insert(self, key):
        self.dic.append(key)
    def find(self, key):
        return key in self.dic

dic = Dictionary()
n = int(input())
for i in range(n):
    cmd = input().split()
    if cmd[0] == "insert":
        dic.insert(cmd[1])
    elif cmd[0] == "find":
        if dic.find(cmd[1]):
            print("yes")
        else:
            print("no")
    else:
        print("wrong command")

Submission status: Time Limit Exceeded

Solution code:
m = 1000000
NIL = 1e9

def h1(key):
    return key % m

def h2(key):
    return 1 + (key % (m - 1))

def h(key, i):
    return (h1(key) + i * h2(key)) % m

def insert(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == NIL:
            t[j] = key
            return j
        else:
            i += 1

def find(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == key:
            return j
        elif t[j] == NIL or i >= m:
            return NIL
        else:
            i += 1

def convert(word):
    result = ""
    for i in word:
        if i == "A":
            result += "1"
        elif i == "T":
            result += "2"
        elif i == "G":
            result += "3"
        else:
            result += "4"
    return int(result)


t = [NIL]*m
n = int(input())
for i in range(n):
    com = input().split()
    key = convert(com[1])
    if com[0] == "insert":
        insert(t, key)
    else:
        if find(t, key) != NIL:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
m = 1000000
NIL = -1

def h1(key):
    return key % m

def h2(key):
    return 1 + (key % (m - 1))

def h(key, i):
    return (h1(key) + i * h2(key)) % m

def insert(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == NIL:
            t[j] = key
            return j
        else:
            i += 1

def find(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == key:
            return j
        elif t[j] == NIL or i >= m:
            return NIL
        else:
            i += 1

def convert(word):
    result = ""
    for i in word:
        if i == "A":
            result += "1"
        elif i == "T":
            result += "2"
        elif i == "G":
            result += "3"
        else:
            result += "4"
    return int(result)


t = [NIL]*m
n = int(input())
for i in range(n):
    com = input().split()
    key = convert(com[1])
    if com[0] == "insert":
        insert(t, key)
    else:
        if find(t, key) != NIL:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
m = 10000
NIL = -1

def h1(key):
    return key % m

def h2(key):
    return 1 + (key % (m - 1))

def h(key, i):
    return (h1(key) + i * h2(key)) % m

def insert(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == NIL:
            t[j] = key
            return j
        else:
            i += 1

def find(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == key:
            return j
        elif t[j] == NIL or i >= m:
            return NIL
        else:
            i += 1

def convert(word):
    result = ""
    for i in word:
        if i == "A":
            result += "1"
        elif i == "T":
            result += "2"
        elif i == "G":
            result += "3"
        else:
            result += "4"
    return int(result)


t = [NIL]*m
n = int(input())
for i in range(n):
    com = input().split()
    key = convert(com[1])
    if com[0] == "insert":
        insert(t, key)
    else:
        if find(t, key) != NIL:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
m = 10000000
NIL = -1

def h1(key):
    return key % m

def h2(key):
    return 1 + (key % (m - 1))

def h(key, i):
    return (h1(key) + i * h2(key)) % m

def insert(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == NIL:
            t[j] = key
            return j
        else:
            i += 1

def find(t, key):
    i = 0
    while True:
        j = h(key, i)
        if t[j] == key:
            return j
        elif t[j] == NIL or i >= m:
            return NIL
        else:
            i += 1

def convert(word):
    result = ""
    for i in word:
        if i == "A":
            result += "1"
        elif i == "T":
            result += "2"
        elif i == "G":
            result += "3"
        else:
            result += "4"
    return int(result)


t = [NIL]*m
n = int(input())
for i in range(n):
    com = input().split()
    key = convert(com[1])
    if com[0] == "insert":
        insert(t, key)
    else:
        if find(t, key) != NIL:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
import sys
l = []
for i in sys.stdin:
    temp = i.split()
    if len(temp) > 1:
        if temp[0] == "insert":
            l.append(temp[1])
        else:
            print("no" if temp[1] not in l else "yes")

Submission status: Time Limit Exceeded

Solution code:
class Tree:
    def __init__(self):
        self.children = {}
        self.end = False

    def __contains__(self, key):
        if len(key) == 0:
            return self.end

        k, *rest = key
        if k not in self.children:
            return False

        return rest in self.children[k]

    def add(self, key):
        if len(key) > 0:
            k, *rest = key
            if k not in self.children:
                self.children[k] = self.__class__()

            self.children[k].add(rest)
        else:
            self.end = True


class Dictionary:
    def __init__(self):
        self.tree = Tree()

    def insert(self, word):
        self.tree.add(word)

    def find(self, word):
        return word in self.tree


def run():
    count = int(input())
    words = Dictionary()

    for _ in range(count):
        command, v = input().split()

        if command == 'insert':
            words.insert(v)
        elif command == 'find':
            if words.find(v):
                print('yes')
            else:
                print('no')
        else:
            raise ValueError()


if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = []
for i in range(n):
    cmd, string = input().split()
    if cmd == "insert":
        d.append(string)
    else:
        if string in d:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
def binarySearch(array, key):
    left = 0
    right = len(array)
    while left < right:
        mid = int((left + right) / 2)
        if key == array[mid]:
            return True
        elif key > array[mid]:
            left = mid + 1
        elif key < array[mid]:
            right = mid
        else:
            print("ERROR")
    return False


n = int(input())
d = []
for i in range(n):
    cmd, string = input().split()
    if cmd == "insert":
        d.append(string)
    else:
        if binarySearch(sorted(d), string):
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
def binarySearch(array, key):
    left = 0
    right = len(array)
    while left < right:
        mid = int((left + right) / 2)
        if key == array[mid]:
            return True
        elif key > array[mid]:
            left = mid + 1
        elif key < array[mid]:
            right = mid
        else:
            print("ERROR")
    return False


def convertor(string):
    result = ""
    for s in string:
        if s == 'A':
            result += "1"
        elif s == 'C':
            result += "2"
        elif s == 'G':
            result += "3"
        elif s == 'T':
            result += "4"
        else:
            print("ERROR")
    return eval(result)


n = int(input())
d = []
for i in range(n):
    cmd, string = input().split()
    string = convertor(string)
    if cmd == "insert":
        d.append(string)
    else:
        if binarySearch(sorted(d), string):
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
order = [input() for _ in range(n)]
dic = []

for i in range(n):
    # insert
    if order[i][0] == "i":
        dic += [order[i][7:]]
    # find
    else:
        dic += [order[i][5:]]
        j = 0
        while dic[j] != order[i][5:]:
            j += 1
        dic.pop()
        if j == len(dic):
            print("no")
            continue
        else:
            print("yes")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
order = [input() for _ in range(n)]
dic = []

for i in range(n):
    if order[i][0] == "i":
        dic += [order[i][7:]]
    else:
        dic += [order[i][5:]]
        j = 0
        while dic[j] != order[i][5:]:
            j += 1
        dic.pop()
        if j == len(dic):
            print("no")
        else:
            print("yes")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
o = []
for i in range(n):
    op, ob = map(str, input().split())
    if op == "insert":
        o.append(ob)
    else:
        if ob in o:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
o = {""}
for i in range(n):
    a = len(o)
    op, ob = map(str, input().split())
    if op == "find":
        ocp = o.copy()
        ocp.add(ob)
        if len(ocp) > a:
            print("no")
        else:
            print("yes")
    else:
        o.add(ob)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
d = deque()
for i in range(n):
    c, s = input().split()
    if c == "insert":
        d.append(s)
    else:
        if s in d:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import bisect
n = int(input())
d = []
for i in range(n):
    c, s = input().split()
    if c == "insert":
        d.insert(bisect.bisect_left(d, s), s)
    else:
        j = bisect.bisect_left(d, s)
        if j < len(d) and d[j] == s:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 30 13:14:10 2018
ALDS1-4c simple implementation
@author: maezawa
"""

n = int(input())
a = []
for s in range(n):
    cmd, word = input().split()
    if cmd == 'insert':
        a.append(word)
    else:
        if word in a:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
M = 1046572

def getkey(text):
    dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
    sum = 0
    p = 1
    for c in text:
        sum += p*dic[c]
        p *= 5
    return sum

def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            return False
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        j = insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
#! python3
# dictionary.py

# ハッシュ法, チェイン法, 開番地法, etc
# オープンアドレス法で実装する

M = 113131

def getkey(text):
    dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
    sum = 0
    p = 1
    for c in text:
        sum += p*dic[c]
        p *= 5
    return sum

def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
#! python3
# dictionary.py

# ハッシュ法, チェイン法, 開番地法, etc
# オープンアドレス法で実装する

M = 8193173

def getkey(text):
    dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
    sum = 0
    p = 1
    for c in text:
        sum += p*dic[c]
        p *= 5
    return sum

def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
M = 15140366

def getkey(text):
    dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
    sum = 0
    p = 1
    for c in text:
        sum += p*dic[c]
        p *= 5
    return sum

def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
import hashlib
M = 15140366

def getkey(text):
    dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
    sum = 0
    p = 1
    for c in text:
        sum += p*dic[c]
        p *= 5
    return sum

dict = {}
def find(text):
    global dict
    # key = getkey(text)
    i = 0
    while i < M:
        h = hashlib.md5(text.encode()).hexdigest()
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    # key = getkey(text)
    i = 0
    while i < M:
        j = hashlib.md5(text.encode()).hexdigest()
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
M = 15140366
def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

word_dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
def getkey(text):
    sum = 0
    p = 1
    for c in text:
        sum += p*word_dic[c]
        p *= 5
    return sum

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
M = 2056197
def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

word_dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
def getkey(text):
    sum = 0
    p = 1
    for c in text:
        sum += p*word_dic[c]
        p *= 5
    return sum

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
M = 1046527
def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

word_dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
def getkey(text):
    sum = 0
    p = 1
    for c in text:
        sum += p*word_dic[c]
        p *= 5
    return sum

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
for i in range(n):
    op, text = input().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
import sys

M = 1046527
def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

word_dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
def getkey(text):
    sum = 0
    p = 1
    for c in text:
        sum += p*word_dic[c]
        p *= 5
    return sum

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
operations = sys.stdin.readlines()
for ope in operations:
    op, text = ope.rstrip().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    x,m = map(str,input().split())
    if x[0] =='i':a.append(m)
    elif x[0] =='f':
        if m in a:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
import sys

M = 1046527
def h1(key):
    return key % M

def h2(key):
    return 1 + (key %(M-1))

def h3(key, i):
    return (h1(key) + i*h2(key)) % M

word_dic = {'A': 1, 'T': 2, 'G': 3, 'C': 4}
def getkey(text):
    sum = 0
    p = 1
    for c in text:
        sum += p*word_dic[c]
        p *= 5
    return sum

dict = {}
def find(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        h = h3(key, i)
        if dict.get(h) == None:
            break
        elif dict[h] == text:
            return True
        i += 1
    return False

def insert(text):
    global dict
    key = getkey(text)
    i = 0
    while i < M:
        j = h3(key, i)
        if dict.get(j) == None:
            dict[j] = text
            break
        else:
            i += 1

n = int(input())
operations = sys.stdin.readlines()
for ope in operations:
    op, text = ope.rstrip().split(' ')
    if op == 'insert':
        insert(text)
    elif op == 'find':
        rst = find(text)
        if rst:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
q = collections.deque()
for i in range(n):
    x = input()
    l = len(x)
    if x[0] == "i":
        q.insert(0,x[7:])
    elif x[0] == "f":
        try:
            q.index(x[5:])
            print("yes")
        except:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = collections.deque()
c = collections.deque()
g = collections.deque()
t = collections.deque()
for i in range(n):
    x = input()
    l = len(x)
    if x[0] == "i":
        if x[7] == "A":
            a.insert(0,x[7:])
        elif x[7] == "C":
            c.insert(0,x[7:])
        elif x[7] == "G":
            g.insert(0,x[7:])
        elif x[7] == "T":
            t.insert(0,x[7:])

    elif x[0] == "f":
        if x[5] == "A":
            try:
                a.index(x[5:])
                print("yes")
            except:
                print("no")
        elif x[5] == "C":
            try:
                c.index(x[5:])
                print("yes")
            except:
                print("no")
        elif x[5] == "G":
            try:
                g.index(x[5:])
                print("yes")
            except:
                print("no")
        elif x[5] == "T":
            try:
                t.index(x[5:])
                print("yes")
            except:
                print("no")

Submission status: Time Limit Exceeded

Solution code:
#coding: Shift_JIS

def hash(str):
	v=0
	b=1
	for s in str:
		if s=='A': v+=b
		elif s=='C': v+=2*b
		elif s=='G': v+=3*b
		elif s=='T': v+=4*b
		b*=5
	return int(v%100)

dic=[[]]*100
n=int(input())
for i in range(n):
	str=input()
	com=str.split()
	key=hash(com[1])
	if com[0]=="insert":
		dic[key].append(com[1])
	elif com[0]=="find":
		if len(dic[key])==0:
			print("no")
		else:
			flag=0
			for s in dic[key]:
				if s==com[1]:
					print("yes")
					flag+=1
					break
			if flag==0: print("no")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

strs = []

for i in range(n):
    s = input().split()
    if(s[0] == 'insert'):
        if(s[1] not in strs):
            strs.append(s[1])
    else:
        if(s[1] in strs):
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
M = 10 ** 6 + 3
HASH_MAP = [None for i in range(M)]


def hash1(key):
    return key % M


def hash2(key):
    return 1 + (key % M-1)


def my_hash(key, i):
    return (hash1(key)+i*hash2(key)) % M


def make_num(s):
    return len(s) + ord(s[0])


def insert(key):
    i = 0
    key_n = make_num(key)
    while True:
        j = my_hash(key_n, i)
        if HASH_MAP[j] is None:
            HASH_MAP[j] = key
            return j

        i += 1


def search(key):
    i = 0
    key_n = make_num(key)
    while True:
        j = my_hash(key_n, i)
        if HASH_MAP[j] == key:
            return j
        elif HASH_MAP[j] is None or i >= M:
            return None

        i += 1


def main():
    n = int(input())

    for i in range(n):
        order = input().split()
        if order[0] == "insert":
            insert(order[1])
        else:
            if search(order[1]) is None:
                print("no")
            else:
                print("yes")


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
M = 10 ** 6 + 3
HASH_MAP = [None for i in range(M)]


def hash1(key):
    return key % M


def hash2(key):
    return 1 + (key % M-1)


def my_hash(key, i):
    return (hash1(key)+i*hash2(key)) % M


def make_num(s):
    return len(s) + ord(s[0]) + ord(s[-1])


def insert(key):
    i = 0
    key_n = make_num(key)
    while True:
        j = my_hash(key_n, i)
        if HASH_MAP[j] is None:
            HASH_MAP[j] = key
            return j

        i += 1


def search(key):
    i = 0
    key_n = make_num(key)
    while True:
        j = my_hash(key_n, i)
        if HASH_MAP[j] == key:
            return j
        elif HASH_MAP[j] is None or i >= M:
            return None

        i += 1


def main():
    n = int(input())

    for i in range(n):
        order = input().split()
        if order[0] == "insert":
            insert(order[1])
        else:
            if search(order[1]) is None:
                print("no")
            else:
                print("yes")


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#coding: Shift_JIS

def hash(str):
	v=0
	b=1
	for s in str:
		if s=='A': v+=b
		elif s=='C': v+=2*b
		elif s=='G': v+=3*b
		elif s=='T': v+=4*b
		b*=5
	return int(v%100000)

dic=[[]]*100000
n=int(input())
for i in range(n):
	str=input()
	com=str.split()
	key=hash(com[1])
	if com[0]=="insert":
		dic[key].append(com[1])
	elif com[0]=="find":
		if len(dic[key])==0:
			print("no")
		else:
			flag=0
			for s in dic[key]:
				if s==com[1]:
					print("yes")
					flag+=1
					break
			if flag==0: print("no")