ハッシュ・テーブルを作成し、検索要素の挿入削除操作を行います.

20834 ワード

注意:1.このブログは参考交流のためだけに使用して、読者は必ず自分で実践して、決して無理をしないでください.筆者のレベルが限られているため、本文に誤りや改善点があれば、コメントエリアでテーマDescriptionを指摘することを歓迎します.
ハッシュテーブルとも呼ばれるハッシュテーブル(Hash table)は、キー値(Key value)に基づいて直接アクセスするデータ構造である.つまり、キー値をテーブルの1つの場所にマッピングすることでレコードにアクセスし、検索を高速化します.このマッピング関数をハッシュ関数と呼び,記録を格納する配列をハッシュリストと呼ぶ.
この問題では、ハッシュ関数:ハッシュ・テーブル(ハッシュ・テーブル長m以下の数pでキーワードを除いたハッシュ・テーブル)を使用してハッシュ・テーブルを作成し、指定された値がハッシュ・リストにあるかどうかを判断します.
注意:この問題では、衝突、すなわち衝突を考慮してください.この問題は、チェーン・バースト法を使用して競合を解決します.つまり、同じアドレスにハッシュした後、サブチェーンを確立する方法で要素競合を解決します.したがって、この問題では、合計要素がハッシュ・テーブルのサイズより大きくオーバーフローする場合は考慮しません.重複要素は1つだけ保存されます.
Inputの1行目の数字は作成するハッシュ・リストのサイズを表し、2行目は要素としてハッシュ・リストを作成する整数のシーケンスを表し(注意:要素間はスペースで区切られており、1行目のハッシュ・リストのサイズはローの要素の数を表していない)、3行目は要素を挿入する数nを表し、次にn行は挿入する要素の値を表す.次の行は削除する要素の数mを表し、次にm行は削除する要素の値を表す.次の行は検索する要素の数rを表し、次にr行は検索する要素の値を表す.
Output挿入は出力がなく、削除すべき要素ごとに、要素がハッシュリストにあれば削除すればよいが、いなければ「Delete Error」を出力し、単語間のスペースに注意する.挿入削除が完了すると、要素がハッシュ・リストにあるかどうかを検索し、該当する行にTrueを出力すると、Falseが出力されます.
Sample Input 1
5 6 9 88 20 5 6 3 4 5 10 3 11 88 10 2 6 10
Sample Output 1
Delete Error True False Hint
注意:総要素数は、ハッシュ・テーブル・サイズを超える場合があります.この問題は、チェーン・バースト法を使用して競合を解決します.つまり、同じアドレスにハッシュした後、サブチェーンを確立する方法で要素競合を解決します.したがって、この問題では、合計要素がハッシュ・テーブルのサイズより大きくオーバーフローする場合は考慮しません.重複要素は1つだけ保存されます.
分析:今回はコメントに書いてみました(コメントの位置が厳密ではなく、説明用のみ)
# -*- coding: utf-8 -*-


class Slot(object):
    def __init__(self, key=None, next_slot=None):
        self.key, self.next_slot = key, next_slot
    # Slot  key  
    
    
class HashTable(object):
    def __init__(self, size=10):
        self._table = [Slot()] * size
        self.size = size
    #      _table,               size   ,          
    
    def find_key(self, key):
        return key % self.size
    #     

    def append(self, key):
        index = self.find_key(key)
        temp_slot = self._table[index]
        if temp_slot.key is not None:
            if key != temp_slot.key:
                while temp_slot.next_slot is not None and key != temp_slot.next_slot.key:
                    temp_slot = temp_slot.next_slot
                if temp_slot.next_slot is None:
                    temp_slot.next_slot = Slot(key)
        else:
            self._table[index] = Slot(key)
        #              ,     Slot(key)                   

    def __delitem__(self, key):
        index = self.find_key(key)
        temp_slot = self._table[index]
        if temp_slot.key is not None:
            if key != temp_slot.key:
                while temp_slot.next_slot is not None and key != temp_slot.next_slot.key:
                    temp_slot = temp_slot.next_slot
                if temp_slot.next_slot is not None:
                    temp_slot.next_slot = temp_slot.next_slot.next_slot
                else:
                    if key != temp_slot.key:
                        print('Delete Error')
                    else:
                        temp_slot.key = None
            else:
                if temp_slot.next_slot is not None:
                    self._table[index] = temp_slot.next_slot
                else:
                    temp_slot.key = None
        else:
            print('Delete Error')
        #                ,              ,  __delitem__         del ...    
        
    def __contains__(self, key):
        index = self.find_key(key)
        temp_slot = self._table[index]
        if temp_slot.key is not None:
            if key != temp_slot.key:
                while temp_slot.next_slot is not None and key != temp_slot.next_slot.key:
                    temp_slot = temp_slot.next_slot
                if temp_slot.next_slot is not None:
                    return True
                else:
                    if key != temp_slot.key:
                        return False
                    else:
                        return True
            else:
                return True
        else:
            return False
        #    del del             __contains__        if key in ...

    @property
    def table(self):
        return self._table
    # @property  table        ,       : h.table[1].key = ...    h.table = ...

    def traverse(self):
        for i in range(self.size):
            temp_slot = self.table[i]
            while temp_slot is not None:
                if temp_slot.key is not None:
                    print('self.table[', i, '].key is ', temp_slot.key)
                temp_slot = temp_slot.next_slot
    #        ,               


n = int(input())

h = HashTable(n)

temp = list(map(int, input().split(" ")))

for i in range(len(temp)):
    h.append(temp[i])

m = int(input())

for i in range(m):
    h.append(int(input()))

k = int(input())

for i in range(k):
    del h[int(input())]

p = int(input())

for i in range(p):
    u = int(input())
    if u in h:
        print('True')
    else:
        print('False')