题意:

给一个字符串 寻找字符串为(a+a)格式的子串有多少。a+a 格式字符串比如 abcabc, ee 等。

首先O(N^2)枚举子串,然后通过哈希在O(1)复杂度判断子串是否符合要求。

RK哈希,Rabin_Karp 哈希,通过比较hash值是否相等来比较每个字符串是否相等。有概率出错(很小)

将字符串看做一个 n 进制的数字,通过一个大质数(如 1000000007 )取模,取得字符串的值。

这里使用无符号 64 位整数来存储哈希值,并通过 C++ 自然溢出的处理方式来取模。

因为有26个字母,选择27进制。注意一点是27进制,'a'不能被视为 0 否则 aa 和 a 就相同了。。。

代码:

class Solution {
public:
int distinctEchoSubstrings(string text) {
typedef unsigned long long ull;
int n = text.size();
int base = 27;
vector<vector<ull>> h(n, vector<ull>(n));
for (int i = 0; i < n; i++) {
h[i][i] = text[i] - 'a' + 1;
for (int j = i + 1; j < n; j++) {
h[i][j] = h[i][j - 1] * base + (text[j] - 'a' + 1);
}
}
set<ull> st;
for (int i = 0; i < n; i++) {
for (int j = 1; i + j * 2 <= n; j++) {
if (h[i][i + j - 1] == h[i + j][i + j * 2 - 1]) {
st.insert(h[i][i + j - 1]);
}
}
}
return st.size();
}
};

参考:

https://www.acwing.com/solution/leetcode/content/7499/

LeetCode 1316. Distinct Echo Substrings (RK哈希)的更多相关文章

  1. RK哈希(Rabin_Karp 哈希)

    Rabin_Karp 哈希通过比较hash值是否相等来比较每个字符串是否相等有概率出错(很小)字符串x1,x2,x3……xk基底e;模数mo;hash=(xk*e^0+xk-1*e^1+......+ ...

  2. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  3. 【LeetCode】1180. Count Substrings with Only One Distinct Letter 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 组合数 日期 题目地址:https://leetcod ...

  4. LeetCode 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  5. 1087: Common Substrings (哈希)

    1087: Common Substrings Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/ ...

  6. Java for LeetCode 115 Distinct Subsequences【HARD】

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. [LeetCode] 115. Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  8. LeetCode 1100. Find K-Length Substrings With No Repeated Characters

    原题链接在这里:https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/ 题目: Give ...

  9. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  10. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

随机推荐

  1. 深度学习 玩游戏 Q-LEARNING

    游戏里面非玩家的角色行为,即 AI. 腾讯的 Ai 游戏框架:TencentOpen. 介绍: Agent,behavior tree, 大概意思就是 通过自己的框架来确定 ai 行为,然后通过 ag ...

  2. windows10使用scp命令

    windows10使用scp命令 windows自带scp命令 windows上传文件到linux//使用方法:scp 源文件路径 账户@地址:目的路径scp   C:\Users\zbh\Deskt ...

  3. ClickHouse的向量处理能力

    ClickHouse的向量处理能力 引言 在过去,非结构化数据(如文本.图片.音频.视频)通常被认为难以在数据库中直接使用,因为这些数据类型的多样性和复杂性.然而,随着技术的发展,嵌入技术可以将非结构 ...

  4. Linux下查看压缩文件内容的 10 种方法【转】

    转载地址:https://zhuanlan.zhihu.com/p/91593509 1.使用 Vim 编辑器 Vim 不仅仅是编辑器,它还包含其他许多强大的功能.下面的命令将直接显示压缩归档文件的内 ...

  5. 【Java】单号创建服务

    需求:ERP项目存在若干个业务功能,每个业务的单子的单号创建规则需要被统一规划 1.每个业务有自己对应的标识 2.业务单号必须以英文字母为前缀,长度在2 - 4个字符 3.单号的组成 = [ 前缀 ] ...

  6. 【MacOS】VMware安装10.15-Catalina版本

    参考自: https://www.bilibili.com/video/BV1sf4y1D77A?p=4 资源地址: https://pan.baidu.com/s/1U6WOorb_TuORQ9ab ...

  7. [rCore学习笔记 021]多道程序与分时任务

    写在前面 本随笔是非常菜的菜鸡写的.如有问题请及时提出. 可以联系:1160712160@qq.com GitHhub:https://github.com/WindDevil (目前啥也没有 导读 ...

  8. ( Ubuntu环境下 )Vim插件推荐-Python自动补齐Vim插件jedi-vim的安装(使用插件管理器vundle进行安装)

    Ubuntu系统下,为 Vim 安装python自动补齐的插件   jedi-vim   . 1.   jedi-vim安装依赖 首先,jedi-vim插件需要当前Vim版本支持python,在终端输 ...

  9. pytorch 第三方模块 GraphNAS 安装成功记录

    实验室的小师妹要安装pytorch的第三方模块,经过多方努力没有安装上,后来我接手后也是感觉头疼. 该模块地址:   https://github.com/GraphNAS/GraphNAS 该模块主 ...

  10. redis集群主从之读写分离

    redis集群主从之读写分离 1.集群部署 这里就不详细赘述如何部署主从集群了,一般都是使用slaveOf配置来进行初始化配置. 2.与springboot集成实现读写分离 通过注解实现调用层读写分离 ...