594 · 字符串查找 II(hash rabin karp)

发布时间:2026/9/8 23:09:55
594 · 字符串查找 II(hash rabin karp) 链接LintCode 炼码 - 更高效的学习体验题解class Solution { public: /** * param source: A source string * param target: A target string * return: An integer as index */ int strStr2(string source, string target) { // write your code here int base 26; int MOD 1000000; int len target.size(); if (len 0) { return 0; } int m source.size(); if (m len) { return -1; } int power 1; for (int i 0; i len - 1; i) { power (power * 26) % MOD; } int target_hash 0; for (auto ch : target) { target_hash (target_hash * base (ch - a)) % MOD; } int source_hash 0; for (int i 0; i len; i) { source_hash (source_hash * base (source[i] - a)) % MOD; } if (source_hash target_hash) { if (source.substr(len) target) { return 0; } } for (int i 1; i len - 1 m; i) { source_hash ((source_hash - (source[i - 1] - a) * power % MOD MOD) % MOD * base (source[i len - 1] - a)) % MOD; if (source_hash target_hash) { if (source.substr(i, len) target) { return i; } } } return -1; } };二维class Solution { public: /** * param matrix: 主串矩阵 * param pattern: 模式串矩阵 * return: 模式串在主串中出现的次数 */ int findPattern(vectorvectorchar matrix, vectorvectorchar pattern) { int n matrix.size(); // 主串行数 int m matrix[0].size(); // 主串列数 int p pattern.size(); // 模式串行数 int q pattern[0].size(); // 模式串列数 if (n p || m q) return 0; const int BASE 26; const long long MOD 1e9 7; // 1. 计算模式串的二维哈希值 long long pattern_hash 0; for (int i 0; i p; i) { long long row_hash 0; for (int j 0; j q; j) { row_hash (row_hash * BASE (pattern[i][j] - a)) % MOD; } pattern_hash (pattern_hash * BASE row_hash) % MOD; } // 2. 计算每行的滚动哈希 vectorvectorlong long row_hashes(n, vectorlong long(m - q 1, 0)); // 计算 BASE^(q-1) long long power_col 1; for (int j 0; j q - 1; j) { power_col (power_col * BASE) % MOD; } for (int i 0; i n; i) { // 计算第一个窗口的哈希值 long long hash 0; for (int j 0; j q; j) { hash (hash * BASE (matrix[i][j] - a)) % MOD; } row_hashes[i][0] hash; // 滚动哈希 for (int j 1; j q m; j) { hash ((hash - (matrix[i][j-1] - a) * power_col % MOD MOD) % MOD * BASE (matrix[i][jq-1] - a)) % MOD; row_hashes[i][j] hash; } } // 3. 在列方向上使用滚动哈希匹配 long long power_row 1; for (int i 0; i p - 1; i) { power_row (power_row * BASE) % MOD; } int result 0; // 枚举所有可能的起始列 for (int j 0; j q m; j) { // 计算第一个窗口的二维哈希值 long long matrix_hash 0; for (int i 0; i p; i) { matrix_hash (matrix_hash * BASE row_hashes[i][j]) % MOD; } if (matrix_hash pattern_hash) { result; } // 滚动哈希 for (int i 1; i p n; i) { matrix_hash ((matrix_hash - row_hashes[i-1][j] * power_row % MOD MOD) % MOD * BASE row_hashes[ip-1][j]) % MOD; if (matrix_hash pattern_hash) { result; } } } return result; } };// 本题解信息来源于【九章算法】。请勿进行商业转载非商业转载请注明出处。 class Solution { public: /** * param source a source string * param target a target string * return an integer as index */ int strStr2(const char* source, const char* target) { // Write your code here if (source NULL || target NULL) return -1; int m strlen(target); int n strlen(source); if (m 0) return 0; int mod rand() % 1000000 1000000; int hash_target 0; int m26 1; for (int i 0; i m; i) { hash_target (hash_target * 26 target[i] - a) % mod; if (hash_target 0) hash_target mod; } for (int i 0; i m - 1; i) m26 m26 * 26 % mod; int value 0; for (int i 0; i n; i) { if (i m) value (value - m26 * (source[i - m] - a)) % mod; value (value * 26 source[i] - a) % mod; if (value 0) value mod; if (i m - 1 value hash_target) { // you have to double check by directly compare the string char sub[m]; memcpy(sub, source[i - m 1], m); sub[m] \0; if (strcmp(target, sub) 0) { return i - m 1; } } } return -1; } };https://www.jiuzhang.com/problems/info/594

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询