【发布时间】:2013-10-30 16:29:52
【问题描述】:
我有如下结构化数据:
struct Leg
{
char type;
char side;
int qty;
int id;
} Legs[5];
在哪里
type is O or E,
side is B or S;
qty is 1 to 9999 and qty in all Legs is relative prime to each other i.e. 1 2 3 not 2 4 6
id is an integer from 1 to 9999999 and all ids are unique in the group of Legs
为了构建上述数据的唯一签名,目前我正在构建一个如下所示的字符串: 首先根据 id 对 Legs 进行排序; 那么
signature=""
for i=1 to 5
signature+=id+type+qty+side of leg-i
然后我插入到 unordered_map 中,这样如果有任何匹配的结构化数据出现,我就可以通过构建上述签名并进行查找来查找。
unorderd_map on string 表示 key-compare 是字符串比较,也是哈希函数,需要遍历通常在 25 个字符左右的字符串。
为了提高效率,可以为上面的每个结构从上面的数据中构建一个唯一的整数,在 unorderd_map 中的查找/插入会非常快。
只是想知道我是否可以利用任何数学属性。
编辑: 该地图将包含键值对,例如
<unique-signature=key, value=int-value needs to be located on looking up another repeating Leg group by constructing signature like above after sorting Legs based on id>
<123O2B234E3S456O3S567O2S789E2B, 989>
我们的目标是从每个这样独特的重复腿组中构建独特的签名。腿可以有不同的顺序,但它们可以与另一组不同顺序的腿匹配,这就是为什么我根据唯一的 id 进行排序并构建签名。
我的签名是基于字符串的,如果有办法构造一个唯一的数字签名,那么我的查找/插入会更快。
【问题讨论】:
标签: c++ algorithm data-structures hash mathematical-optimization