其他人提到了二进制搜索和哈希映射。我怀疑有一个稍微快一点的方法,因为你不关心内存使用的效率。对您要查找的数据进行桶排序并与之进行比较。这应该会加快比较速度——而不是散列完整的输入然后匹配,它总是在寻找一个完全匹配的。
鉴于地图,{ABC, ABD, DEF, GH}
您的数据结构看起来像......
pos: 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 <-- slot 26 is reserved for
val: A 0 0 D 0 0 G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "end of map token"
| | |
B E H <-- sub buckets don't show all buckets as in the top level, but they
/ \ | | are all there.
C D F END
/ | \
END END END
扩展您接受的完整字符集,或每个字节 255 个可能的值。然后,当您获得输入时,您可以测试每个字符。
示例 1:
Input: ABC
1: look up location 'Z' - 'A' = 0
2: is there anything in this bucket? yes
3: look in sub-buckets.
4: Is there anything in the 'Z'- 'B' bucket? yes
5: look in sub buckets.
6: Is there anything in the 'Z' - 'C' bucket? yes
7: look in sub-buckets
8: reached end of input token. Is end of map bucket is marked? yes
9: match found
示例 2:
Input: ABCD (input token longer than closest match in map)
1: look up location 'Z' - 'A' = 0
2: is there anything in this bucket? yes
3: look in sub-buckets.
4: Is there anything in the 'Z'- 'B' bucket? yes
5: look in sub buckets.
6: Is there anything in the 'Z' - 'C' bucket? yes
7: look in sub-buckets
8: Is there anything in the 'Z' - 'D' bucket? no
9: match not found
示例 3:
Input: ABE
1: look up location 'Z' - 'A' = 0
2: is there anything in this bucket? yes
3: look in sub-buckets.
4: Is there anything in the 'Z'- 'B' bucket? yes
5: look in sub buckets.
6: Is there anything in the 'Z' - 'E' bucket? no
7: match found