pidgeonholeを考えてください。この方法は、すべての場合でO(m + n)の複雑さです。
各「取引」を一連のルールに結合し、アイテムをループして、ルールが満たされる各pidgeonholeに追加します。
次に、取引をループし、アイテムがピジョンホールにある場合はピジョンホールからアイテムを引き出します。
例:
"blue car", "red car", "yellow expensive car", "red expensive car", "cheap car"
お得な情報:
buy a red car, get a blue car for free
buy an expensive car, get a cheap car for free
推測できる「ルール」:
is_red, is_expensive, is_blue, is_cheap
したがって、is_redとis_expensiveの2つの穴があります。アイテムをループして、ルールが満たされているすべての穴に追加し、次の穴を作成します。
is_red ( "red car", "red expensive car" )
is_expensive ( "red expensive car", yellow expensive car" )
is_blue ( "blue car" )
is_cheap ( "cheap car" )
次に、取引をループします。
buy a red car, get a blue car for free
is_red contains "red car", is_blue contains "blue car", so:
pop them from the holes and make them a deal
次の取引:
buy an expensive car, get a cheap car for free
is_expensive doesn't contain any cars