《0.字符串hash以及7大问题1.pptVIP

  • 4
  • 0
  • 约1.07万字
  • 约 39页
  • 2017-01-01 发布于北京
  • 举报
《0.字符串hash以及7大问题1

* * By peterpan 字符串hash简介 有关字符串hash的 7个问题! Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 字符串hash 关于字符串hash,就一句话: 把字符串有效地转化为一个整数 在计算机里,用的是二进制编码。在很多语言里,都是用数字作为数组的下标。因为用数字来存储、表达一个object非常方便。 如果能有一种算法,把每个字符串有效地、”唯一”的映射到每个“不同”的整数,我们就能很好的处理字符串。 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. hash函数 现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数上 比如hash[i]=(hash[i-1]*p+idx(s[i]))%mod 字符串:abc,bbc,aba,aadaabac 字符串下标从0开始 先把a映射为1,b映射为2,c-3,d-4,即idx(a)=1, idx(b)=2, idx(c)=3,idx(d)=4; 好!开始对字符串进行hash Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. hash函数 假设我们取p=13 ,mod=101 先把abc映射为一个整数 hash[0]=1,表示 a 映射为1 hash[1]=(hash[0]*p+idx(b))%mod=15,表示 ab 映射为 15 hash[2]=(hash[1]*p+idx(c))%mod=97 这样,我们就把 abc 映射为 97 这个数字了。 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. hash函数 用同样的方法,我们可以把bbc,aba,aadaabac都映射到一个整数 用同样的hash函数,得到如下结果 abc - 97 bbc - 64 aba - 95 aadaabac - 35 那么,我们发现,这是一个字符串到整数的映射 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. hash函数 这样子,我们就可以记录下每个字符串对应的整数,当下一次出现了一个已经出现的字符串时,查询整数是否出现过,就可以知道 字符串是否重复出现。 现在要判断两个字符串是否一致,怎么办呢?直接用它们的hash值判断即可,若hash值一致,则认

文档评论(0)

1亿VIP精品文档

相关文档