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:
def f(s,t):
 #   As="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    As="ACGT"
    if s==t:
        return 0
    else:
        i=0
        while i<4 and s!=As[i]:
            i+=1
        while i<4 and t!=As[i]:
            i+=1
        if i<4:
            return -1
        else:
            return 1
def f3(u,v):
    m=len(u)
    if len(v)<m:
        m=len(v)
    i=0
    while i<m and u[i]==v[i]:
        i+=1
    if i>=m:
        if len(u)==len(v):
            return 0
        elif m==len(u):
            return -1
        else:
            return 1
    else:
        return f(u[i],v[i])
n=int (input ())
D=[]
for i in range(n):
    Od=input (). split ()
    l=len(D)
    gs=Od[1]
    j=0
    ts=1
    if l>0:
        p=1
        m=l//2
        j+=m
        ts=f3(gs,D[j])
    #    print(ts)
        while m>1 and ts!=0:#ight over
            if ts*p>0:
                l-=m+1
            else:
                l=m
            m=l//2
            if ts<0:
                j-=m+1
                p=-1
            else:
                j+=m+1
                p=1
            ts=f3(gs,D[j])
        if ts*p>0:
            l-=m+1
        else:
            l=m
        if l==1 and ts!=0:
            j+=ts
            ts=f3(gs,D[j])
    if Od[0]=="insert":
       # print(gs)
        if ts==-1:
            ts=0
        D.insert(j+ts,gs)
  #      print(D)
    elif ts==0:
        print("yes")
    else:
        print("no")

Submission status: Time Limit Exceeded

Solution code:
#dictionary
n=int (input ())
D=[]
for i in range(n):
    Od=input (). split ()
    if Od[0]=="insert":
        D.append(Od[1])
    else:
        l=len(D)
        gs=Od[1]
        j=0
        while j<l and gs!=D[j]:
            j+=1
        if j>=l:
            print("no")
        else:
            print("yes")

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
L = []

for i in xrange(n):
    a, b = raw_input().split()
    if a == "insert":
        L.append(b)
    if a == "find":
        if b in L:
            print "yes"
        else:
            print "no"

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = []
for i in range(n):
    a,b = map(str,raw_input().split())
    if a == "insert":
        lst.append(b)
    elif b in lst:
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dic = [0]*n

def hash1(x):
    return (x % n)
def hash2(x):
    return 1 + (x % (n-1))

for i in range(n):
    line = input().split()
    com = line[0]
    st = str(line[1])
    su = 0
    for i in st:
        if i == 'A':
            su += 1
        elif i == 'C':
            su += 2
        elif i == 'G':
            su += 3
        else:
            su += 4
    if com == 'insert':
        ind = hash1(su)
        while True:
            if dic[ind] == 0:
                dic[ind] = st
                break
            else:
                ind = (ind + hash2(su)) % n
    else:
        ind = hash1(su)
        while True:
            if dic[ind] == st:
                print('yes')
                break
            elif dic[ind] == 0:
                print('no')
                break
            else:
                ind = (ind + hash2(su)) % n

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []

for i in range(n):
    line = input().split()
    if line[0] == "insert":
        l.append(line[1])
    elif line[0] == "find":
        if line[1] in l:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = []

for i in range(n):
    line = input().split()
    if line[0] == "insert":
        d.append(line[1])
    elif line[0] == "find":
        if line[1] in d:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
n = int(input().strip())
op = ''
elem = ''
set_of_elem = []
for i in range(n):
	op,elem = input().split()
	if op == 'insert':
		set_of_elem.append(elem)
	elif op == 'find':
		if elem in set_of_elem:
			print('yes')
		else:
			print('no')

Submission status: Time Limit Exceeded

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


class Cell(object):
    __slots__ = ["value", "pre", "next"]

    def __init__(self, value=None, pre=None, _next=None):
        self.value = value
        if pre is None and _next is None:
            self.pre = self
            self.next = self
        else:
            self.pre = pre
            self.next = _next


class DoublyLinkedList(object):
    def __init__(self):
        self._nil = Cell()

    def insert(self, value):
        self._nil.next = Cell(value, self._nil, self._nil.next)
        self._nil.next.next.pre = self._nil.next

    def toList(self):
        gen = self._val_generator()
        return [val for val in gen()]

    def _val_generator(self):
        def gen():
            current = self._nil.next
            while current is not self._nil:
                yield current.value
                current = current.next
        return gen

    def find(self, value):
        current = self._nil.next
        while current is not self._nil and current.value != value:
            current = current.next
        if current is self._nil:
            return False
        else:
            return True


class Hash(object):
    def __init__(self, size):
        self.array = [None] * size
        self.size = size

    def _hash(self, i):
        return i % self.size

    def insert(self, i, value):
        _hash = self._hash(i)
        if self.array[_hash] is None:
            self.array[_hash] = DoublyLinkedList()
        self.array[_hash].insert(value)

    def find(self, i, value):
        _hash = self._hash(i)
        if self.array[_hash] is None:
            return False
        else:
            return self.array[_hash].find(value)


def ctoi(c):
    if c == "A":
        return 1
    elif c == "C":
        return 2
    elif c == "G":
        return 3
    elif c == "T":
        return 4


def stoi(s):
    ret = 0
    for i, c in enumerate(s, 1):
        ret += ctoi(c) * i
    return ret


def dictionary(lst):
    h = Hash(100)
    for val in lst:
        if val[0] == "insert":
            h.insert(stoi(val[1]), val[1])
        elif val[0] == "find":
            if h.find(stoi(val[1]), val[1]):
                print("yes")
            else:
                print("no")
        else:
            raise ValueError


if __name__ == "__main__":
    lst = [val.split() for val in sys.stdin.read().splitlines()]
    lst.pop(0)
    dictionary(lst)

Submission status: Time Limit Exceeded

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


class Hash(object):
    def __init__(self, size):
        self._array = [None] * size
        self._size = size

    def _h1(self, key):
        return key % self._size

    def _h2(self, key):
        return 1 + (key % (self._size - 1))

    def _hash(self, key, i):
        return (self._h1(key) + i * self._h2(key)) % self._size

    def insert(self, key, value):
        i = 0
        while 1:
            j = self._hash(key, i)
            if self._array[j] is None:
                self._array[j] = value
                break
            else:
                i += 1

    def find(self, key, value):
        i = 0
        while 1:
            j = self._hash(key, i)
            if self._array[j] == value:
                return True
            elif self._array[j] is None or i >= self._size:
                return False
            else:
                i += 1


def stoi(s):
    ret = 0
    p = 1
    ctoi = {"A": 1, "C": 2, "G": 3, "T": 4}
    for c in s:
        ret += ctoi[c] * p
        p *= 5
    return ret


def dictionary(lst):
    h = Hash(1046527)
    ret = []
    for val in lst:
        if val[0] == "insert":
            h.insert(stoi(val[1]), val[1])
        elif val[0] == "find":
            if h.find(stoi(val[1]), val[1]):
                ret.append("yes")
            else:
                ret.append("no")
        else:
            raise ValueError
    return ret


if __name__ == "__main__":
    lst = [val.split() for val in sys.stdin.read().splitlines()]
    n = int(lst.pop(0)[0])
    print("\n".join(dictionary(lst)))

Submission status: Time Limit Exceeded

Solution code:
class my_dict:

    def __init__(self):
        self.d = []

    def insert(self, x):
        if (x not in self.d):
            self.d.append(x)

    def find(self, x):
        if (x in self.d):
            return 'yes'
        else:
            return 'no'


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

    for i in range(n):
        command, string = input().split()
        if (command == 'insert'):
            my_d.insert(string)
        elif (command == 'find'):
            print(my_d.find(string))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dict , ans = [] , []

for i in range(n):
    x , y = input().split()
    if x == "insert":
        dict.append(y)
    else:
        if y in dict:
            ans.append("yes")
        else:
            ans.append("no")
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = []
for i in range(n):
    op = input().split()
    if op[0] == 'insert':
        d.append(op[1])  
    elif op[0] == 'find':
        if op[1] in d:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
d = []
for op in sys.stdin.readlines():
    op = op.split()
    if op[0] == 'insert':
        d.append(op[1])  
    elif op[0] == 'find':
        if op[1] in d:
            print('yes')
        else:
            print('no')

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
a_dict = [[],[],[],[],[]]
c_dict = [[],[],[],[],[]]
t_dict = [[],[],[],[],[]]
g_dict = [[],[],[],[],[]]

for i in range(n):
	cmd = raw_input().split()
	char = cmd[1]
	
	if cmd[0] == "insert":
		if char[0] == "A":
			if len(char) == 1:
				a_dict[0].append(char)
			elif char[1] == "A":
				a_dict[1].append(char)
			elif char[1] == "T":
				a_dict[2].append(char)
			elif char[1] == "C":
				a_dict[3].append(char)
			else:
				a_dict[4].append(char)
		elif char[0] == "T":
			if len(char) == 1:
				t_dict[0].append(char)
			elif char[1] == "A":
				t_dict[1].append(char)
			elif char[1] == "T":
				t_dict[2].append(char)
			elif char[1] == "C":
				t_dict[3].append(char)
			else:
				t_dict[4].append(char)
		elif char[0] == "C":
			if len(char) == 1:
				c_dict[0].append(char)
			elif char[1] == "A":
				c_dict[1].append(char)
			elif char[1] == "T":
				c_dict[2].append(char)
			elif char[1] == "C":
				c_dict[3].append(char)
			else:
				c_dict[4].append(char)
		else:
			if len(char) == 1:
				g_dict[0].append(char)
			elif char[1] == "A":
				g_dict[1].append(char)
			elif char[1] == "T":
				g_dict[2].append(char)
			elif char[1] == "C":
				g_dict[3].append(char)
			else:
				g_dict[4].append(char)
	else:
		if char[0] == "A":
			if len(char) == 1:
				if char in a_dict[0]:
					print("yes")
				else:
					print("no")
			elif char[1] == "A":
				if char in a_dict[1]:
					print("yes")
				else:
					print("no")
			elif char[1] == "T":
				if char in a_dict[2]:
					print("yes")
				else:
					print("no")
			elif char[1] == "C":
				if char in a_dict[3]:
					print("yes")
				else:
					print("no")
			else:
				if char in a_dict[4]:
					print("yes")
				else:
					print("no")
		elif char[0] == "T":
			if len(char) == 1:
				if char in t_dict[0]:
					print("yes")
				else:
					print("no")
			elif char[1] == "A":
				if char in t_dict[1]:
					print("yes")
				else:
					print("no")
			elif char[1] == "T":
				if char in t_dict[2]:
					print("yes")
				else:
					print("no")
			elif char[1] == "C":
				if char in t_dict[3]:
					print("yes")
				else:
					print("no")
			else:
				if char in t_dict[4]:
					print("yes")
				else:
					print("no")
		elif char[0] == "C":
			if len(char) == 1:
				if char in c_dict[0]:
					print("yes")
				else:
					print("no")
			elif char[1] == "A":
				if char in c_dict[1]:
					print("yes")
				else:
					print("no")
			elif char[1] == "T":
				if char in c_dict[2]:
					print("yes")
				else:
					print("no")
			elif char[1] == "C":
				if char in c_dict[3]:
					print("yes")
				else:
					print("no")
			else:
				if char in c_dict[4]:
					print("yes")
				else:
					print("no")
		else:
			if len(char) == 1:
				if char in g_dict[0]:
					print("yes")
				else:
					print("no")
			elif char[1] == "A":
				if char in g_dict[1]:
					print("yes")
				else:
					print("no")
			elif char[1] == "T":
				if char in g_dict[2]:
					print("yes")
				else:
					print("no")
			elif char[1] == "C":
				if char in g_dict[3]:
					print("yes")
				else:
					print("no")
			else:
				if char in g_dict[4]:
					print("yes")
				else:
					print("no")

Submission status: Time Limit Exceeded

Solution code:
def main():
    count = int(input())

    imps = []
    for n in range(count):
        imps.append(input())

    db = []
    for imp in imps:
        command = imp.split(' ')
        if command[0] == 'insert':
            db.append(command[1])
        elif command[0] == 'find':
            if command[1] in db:
                print('yes')
            else:
                print('no')


main()

Submission status: Time Limit Exceeded

Solution code:
import sys


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

    imps = sys.stdin.readlines()

    db = []
    for imp in imps:
        command = imp.split(' ')
        if command[0] == 'insert':
            db.append(command[1])
        elif command[0] == 'find':
            if command[1] in db:
                print('yes')
            else:
                print('no')


main()

Submission status: Time Limit Exceeded

Solution code:
import sys


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

    imps = sys.stdin.readlines()

    db = []
    for imp in imps:
        command = imp.split(' ')
        if command[0] == 'insert':
            db.append(command[1])
        else:
            if command[1] in db:
                print('yes')
            else:
                print('no')


main()

Submission status: Time Limit Exceeded

Solution code:
def find(B,J):
    for i in range(len(J)):
        if B==J[i]:
            return 1
    return 0

A=[]
B=[]
J=[]

n=int(input())

for i in range(n):
    a,b=map(str, input().split())
    A.append(a)
    B.append(b)

for i in range(n):
    if A[i]=="insert":
        J.append(B[i])
    else:
        if find(B[i],J)==1:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
def find(B,J):
    if J==[]:
        return 0

    L=0
    R=len(J)

    while L<R:
        M = int((L+R)/2)
        if B==J[M]:
            return 1
        if B<J[M]:
            R=M
        elif B>J[M]:
            L=M+1

A=[]
B=[]
J=[]

n=int(input())

for i in range(n):
    a,b=map(str, input().split())
    A.append(a)
    B.append(b)

for i in range(n):
    if A[i]=="insert":
        J.append(B[i])
    elif A[i]=="find":
        J=sorted(J)
        if find(B[i],J)==1:
            print("yes")
        else:
            print("no")

Submission status: Time Limit Exceeded

Solution code:
def find(B,J):
    if J==[]:
        return 0

    L=0
    R=len(J)

    while L<R:
        M = int((L+R)/2)
        if B==J[M]:
            return 1
        if B<J[M]:
            R=M
        elif B>J[M]:
            L=M+1
    return 0

A=[]
B=[]
J=[]

n=int(input())

for i in range(n):
    a,b=map(str, input().split())
    if a=="insert":
        J.append(b)
    elif a=="find":
        J=sorted(J)
        B.append(find(b,J))

for i in range(len(B)):
    if B[i]==1:
        print("yes")
    else:
        print("no")

Submission status: Time Limit Exceeded

Solution code:
li = []
for _ in range(int(input())):
    c, s = input().split()
    if c == "insert":
        li.append(s)
    elif c == "find":
        print("yes" if s in li else "no")

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline
li = []
for _ in range(int(readline())):
    c, s = readline().split()
    if c == "insert":
        li.append(s)
    elif c == "find":
        print("yes" if s in li else "no")

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



if __name__ == '__main__':

    n = int(input())
    T = HashTable(n)
    char2num = str.maketrans("ACGT", "1234")

    for _ in range(n):
        command, char = input().split(" ")
        num = int(char.translate(char2num))
        if command == "insert":
            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 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))

if __name__ == '__main__':

    n = int(input())
    T = HashTable(n)
    C = [input().split(" ") for i in range(n)]
    
    C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, num in C:
        if command == "insert":
            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 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))

if __name__ == '__main__':

    n = int(input())
    T = HashTable(n)
    C = map(lambda x:(x[0], getNum(x[1])), [input().split(" ") for i in range(n)])
    
    for command, num in C:
        if command == "insert":
            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 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))

if __name__ == '__main__':

    n = int(input())
    T = HashTable(n*10)
    C = [input().split(" ") for i in range(n)]
    C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, num in C:
        if command == "insert":
            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 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))

if __name__ == '__main__':

    n = int(input())
    T = HashTable(n*30)
    C = [input().split(" ") for i in range(n)]
    C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, num in C:
        if command == "insert":
            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 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(n*10)
    C = [input().split(" ") for i in range(n)]
    C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, num in C:
        if command == "insert":
            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 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(n)
    C = [input().split(" ") for i in range(n)]
    C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, num in C:
        if command == "insert":
            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 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)]
    C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, num in C:
        if command == "insert":
            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 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)]
    #C = map(lambda x:(x[0], getNum(x[1])), C)
    
    for command, char in C:
        num = getNum(char)
        if command == "insert":
            T.insert(num)
        else:
            if T.search(num) is None:
                print("no")
            else:
                print("yes")