博客
关于我
字符串哈希-Isomorphic Strings-CF985F
阅读量:368 次
发布时间:2019-03-04

本文共 1525 字,大约阅读时间需要 5 分钟。

为了解决这个问题,我们需要判断两个子串是否是等价的。具体来说,给定一个母串和多个查询,每个查询询问两个子串是否可以一一对应。我们可以使用滚动哈希技术来高效地解决这个问题。

方法思路

  • 滚动哈希预处理:我们首先预处理母串的前缀哈希值和底数幂次数组。前缀哈希数组用于快速计算任意子串的哈希值,底数幂次数组用于处理哈希值的模运算。
  • 哈希值计算:对于每个查询,计算两个子串的哈希值。哈希值相同意味着这两个子串是等价的。
  • 冲突处理:为了减少哈希冲突,我们选择了大质数作为模数,并在计算哈希值时进行适当的模运算。
  • 解决代码

    #include 
    #include
    #include
    #include
    using namespace std;#define ll long longconst int N = 2 * 100000 + 10;const int base = 131;const int mod = 19260817;int n, m;string s;ll p[N], h[N];int main() { scanf("%d %d", &n, &m); scanf("%s", s.c_str()); p[0] = 1; for (int i = 1; i <= n; ++i) { p[i] = (p[i-1] * base) % mod; } for (int i = 1; i <= n; ++i) { ll val = (s[i-1] - 'a' + 1); h[i] = (h[i-1] * base + val) % mod; } for (int i = 0; i < m; ++i) { int x, y, len; scanf("%d %d %d", &x, &y, &len); if (x + len - 1 > n || y + len - 1 > n) { printf("NO"); continue; } ll hash_a = (h[x + len - 1] - (h[x - 1] * p[len]) % mod) % mod; if (hash_a < 0) hash_a += mod; ll hash_b = (h[y + len - 1] - (h[y - 1] * p[len]) % mod) % mod; if (hash_b < 0) hash_b += mod; if (hash_a == hash_b) { printf("YES"); } else { printf("NO"); } } return 0;}

    代码解释

  • 预处理:首先读取输入并预处理底数幂次数组p和前缀哈希数组hp数组存储了底数的幂次模运算结果,h数组存储了母串前缀哈希值。
  • 查询处理:对于每个查询,计算两个子串的哈希值。如果两个哈希值相同,则输出"YES",否则输出"NO"。
  • 模运算处理:在计算哈希值时,使用适当的模运算确保结果正确,并处理负数情况。
  • 这种方法的时间复杂度为O(n)预处理和O(m)查询,能够高效处理大规模输入。

    转载地址:http://cjor.baihongyu.com/

    你可能感兴趣的文章
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>