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

Solution in Scala (judge verdict: Runtime Error)

import scala.io.StdIn
object Main extends App {
  
  val m = 1046527
  val arrayMax = 1000000
  
  //def ctoi(data: String) = data.foldLeft(0)((acc, e) => acc+e)
  def ctoi(data: String) = data.zipWithIndex.foldLeft(0){ (acc, e) =>
    acc + e._2 * (if(e._1 == 'A') 1 else if(e._1 == 'C') 2 else if(e._1 == 'G') 3 else 4)
  }
  
  // A: 65, C:67, G:71, T:84
  def makeHash1(key: Int) = {
    key % m
  }

  def makeHash2(key: Int) = {
    1 + key % (m - 1)
  }
  
  //def makeHash(key: Int, i: Int) = (makeHash1(key) + i * makeHash2(key)) % m
  //def makeHash(key: Int, i: Int) = makeHash1(key) + i
  
  val n = StdIn.readLine().trim.toInt
  
  val dict = Array.fill(arrayMax + 10)("")
  
  def insert(data: String): Unit = {
    var i = 0
    //val key = ctoi(data)
    var colision = true
    while(colision){
      //val j = makeHash(key, i)
      val j = (data.hashCode.abs + i) % arrayMax
      if(dict(j) == "") {
        dict(j) = data
        colision = false
      } else if(dict(j) == data){
        colision = false
      }
      else {
        i = i + 1
      } 
    }
  }

  def search(data: String): Int = {
    //val key = ctoi(data)
    var i = (data.hashCode.abs) % arrayMax
    var ret = -1
    while(ret < 0 && i < arrayMax){
      if(dict(i) == data) {
        ret = i
      }
      i += 1
    }
    ret
  }

  val result = (0 until n).foldLeft(List.empty[String]){ (acc, _) =>
    val cmd = StdIn.readLine().split(' ')
    if(cmd(0) == "insert"){
      insert(cmd(1))
      acc
    } else {
      if(search(cmd(1)) >= 0) {
        "yes"::acc
      } else {
        "no"::acc
      }
    }
  }
  println(result.reverse.mkString(f"%n"))

}