LeetCode 1316. Distinct Echo Substrings (RK哈希)
题意:
给一个字符串 寻找字符串为(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哈希)的更多相关文章
- RK哈希(Rabin_Karp 哈希)
Rabin_Karp 哈希通过比较hash值是否相等来比较每个字符串是否相等有概率出错(很小)字符串x1,x2,x3……xk基底e;模数mo;hash=(xk*e^0+xk-1*e^1+......+ ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】1180. Count Substrings with Only One Distinct Letter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 组合数 日期 题目地址:https://leetcod ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 1087: Common Substrings (哈希)
1087: Common Substrings Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/ ...
- 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 ...
- [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 ...
- LeetCode 1100. Find K-Length Substrings With No Repeated Characters
原题链接在这里:https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/ 题目: Give ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
随机推荐
- 跟着ChatGPT学习设计模式 - 工厂模式
1. 前言 在工作过程中,越发觉得设计模式的重要性.经常会有人说工作5年的人,大学生随便培训1-2月也能做同样的工作,没错,大学生的确可以做. 但其写的代码,可维护性.可扩展性.添加新功能时方便还是简 ...
- 【摘译+整理】System.IO.Ports.SerialPort使用注意
远古的一篇博客,内容散落于博文和评论 https://sparxeng.com/blog/software/must-use-net-system-io-ports-serialport C# 和 . ...
- Golang 高性能 Websocket 库 gws 使用与设计(一)
前言 大家好这里是,白泽,这期分析一下 golang 开源高性能 websocket 库 gws. 视频讲解请关注B站:白泽talk 介绍 gws:https://github.com/lxzan/g ...
- M1 Mac安装anaconda3
1.正常安装 首先进入官网https://www.anaconda.com/ 下载,安装 自行大胆的安装 2.环境配置 直接安装完成后,代码文件的存储路径为默认路径,为了更好的管理代码文件我们需要更换 ...
- 基于 SASL/SCRAM 让 Kafka 实现动态授权认证
一.说明 在大数据处理和分析中 Apache Kafka 已经成为了一个核心组件.然而在生产环境中部署 Kafka 时,安全性是一个必须要考虑的重要因素.SASL(简单认证与安全层)和 SCRAM(基 ...
- 【JS】03 BOM 浏览器对象模型
BOM :Broswer Object Model 浏览器对象模型 核心对象是window对象,window对象又可以操作以下的常见对象: - frames[] 窗口对象数组? 浏览器可以打开多个窗口 ...
- pytorch-a2c-ppo-acktr-gail 算法代码
地址: https://github.com/ikostrikov/pytorch-a2c-ppo-acktr-gail
- 【转载】 softmax_cross_entropy_with_logits中“logits”是个什么意思?
原文地址: https://zhuanlan.zhihu.com/p/51431626 -------------------------------------------------------- ...
- 网络问题解决:Ubuntu连接局域网中Windows主机上的v2r报错:rejected core/proxy/socks: unknown Socks version: 67
参考: https://github.com/2dust/v2rayN/issues/3916 https://www.eolink.com/news/post/30941.html ======== ...
- 代码随想录Day5
242.有效的字母异位词 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词. 示例 ...