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/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
随机推荐
- Known框架实战演练——进销存基础数据
本文介绍如何实现进销存管理系统的基础数据模块,基础数据模块包括商品信息.供应商管理和客户管理3个菜单页面.供应商和客户字段相同,因此可共用一个页面组件类. 项目代码:JxcLite 开源地址: htt ...
- AppiumDesktop控制手机和安卓模拟器
前言: 本期内容 如何用AppiumDesktop连接安卓手机和安卓模拟器 AppiumDesktop基本参数的获取方法,及如何驱动安卓设备 AppiumDesktop在模拟登陆和爬虫中用到的基本功能 ...
- 加速 Mac 时间机器备份
加速 Mac 时间机器备份速度教程,Time Machine 备份太慢的解决方法 @Pertim 2020-09-11 相信用过一段时间电脑的人,都知道经常备份 macOS 系统的重要性了.特别是最近 ...
- 【SVN】文件解锁
提交代码莫名其妙的把文件上锁了 然后找到文件右键的SVN的选项也不能解锁: 原来是这样解锁的: 对上锁文件的所在目录右键找到SVN选项 然后勾选第二项: 这样就解锁了.如果还说没有解锁,说明是对方自己 ...
- 【0基础】从零开始电子DIY!
相关: 视频地址: https://www.bilibili.com/video/BV1xv4y137LL/ github地址: https://github.com/zhangwenchao1992 ...
- 鹏程实验室,启智平台,openI平台,积分兑换新标准
2024-02-13 11:12:21 星期二 地址: https://openi.pcl.ac.cn/reward/point/rule
- 在计算框架MindSpore中手动保存参数变量(Parameter 变量)—— from mindspore.train.serialization import save_checkpoint
本文参考内容: https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/advanced_usage_of_checkpoint.html? ...
- 【转载】 Pytorch手动释放显存
原文地址: http://www.shijinglei.com/2020/04/20/pytorch%E9%87%8A%E6%94%BE%E6%98%BE%E5%AD%98/ ============ ...
- Git 使用中的一些例子
本文紧接前文: .gitignore文件的使用方法(学习总结版)-- .gitignore 文件的配合用法 ============================================= ...
- 增强用户体验:2个功能强大的.NET控制台应用帮助库
前言 对于.NET开发者而言,构建控制台应用程序时,如何提升用户交互的流畅性和满意度,是一个持续探索与优化的话题.今天大姚给大家分享2个功能强大的.NET控制台应用帮助库,希望可以帮助大家能够快速的构 ...