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/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
随机推荐
- 空间反演对称性 (Spatial Inversion Symmetry) 和非线性响应 (Non-linear Response)
我们定义一次宇称变换 (parity transformation) 为反转所有坐标: \[\mathcal{P}: \begin{pmatrix} x \\ y \\ z \end{pmatrix} ...
- neo4j常用命令练习
查询 查询node labels下的节点 match (n:`企业`) return n 查询单个节点,这里返回的是一个节点,具有以下两种写法:在java中是optional,需要get一下 matc ...
- Windows报错0xc0000142的处理方法
1.问题描述 桌面有个弹窗报错(0xc0000142),无法关闭,影响Windows的使用 此错误很有可能是缺少组件导致 2.解决办法 进入" C:\Windows\System32 &qu ...
- 【Java】Springboot + Redis +(AOP & 响应外切)切面实现字典翻译
使用案例演示: 先开发了一个简单的Demo: 普通DTO类注解翻译的字段和翻译来源 在需要翻译的方法上注解@Translate 接口返回结果: 框架思路: 1.标记的注解需要通过AOP切面在调用的时候 ...
- 某宝购入牙膏厂U后其售后事宜的思虑
近日某宝购入i7 9700k, 突然想去保修的问题(没有发票,要发票比京东自营贵200左右, 不要则便宜100左右),由于确实囊中幸亏所以还是选择了某宝,东西到手后突然想起这东西没票是否可以保修 ...
- 国产CPU,国产操作系统UOS——零刻LZX迷你主机 , 显卡驱动安装以及屏幕配置
看网上新闻发现了一款mini电脑--零刻LZX迷你主机 国产兆芯四核八线程 教学家用办公全能王8+256G 该款电脑使用的是国产CPU兆芯,以及国产操作系统UOS,由于价格还不贵就入手玩玩. 商品地址 ...
- model.train方法的dataset_sink_mode参数设置为False时以step作为单位打印数据——(只在mode=context.GRAPH_MODE下成立,在mode=context.PYNATIVE_MODE模式下不成立)
如题: 官方中的内容支持: https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/summary_record.html ...
- openAI的仿真环境Gym Retro的Game Integration——新游戏融合(2)( 示例 demo )
内容接前文: openAI的仿真环境Gym Retro的Game Integration--新游戏融合(将retro中没有融合的ROM游戏加入其中) 前文大致简单的介绍了gym retro 库对新游戏 ...
- mini_imagenet 数据集生成工具 (续)
续接前文: mini_imagenet 数据集生成工具 ============================================ 前文接受了mini_imagenet数据集的生成,但 ...
- springboot项目启动时禁止Redis、数据对象加载
1.背景 2.实现方式 启动类上添加需要排除的自动装配对象 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, H ...