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/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
随机推荐
- Nuxt.js必读:轻松掌握运行时配置与 useRuntimeConfig
title: Nuxt.js必读:轻松掌握运行时配置与 useRuntimeConfig date: 2024/7/29 updated: 2024/7/29 author: cmdragon exc ...
- pip升级导致报错:pip消失
pip升级导致报错:pip消失 在安装Jupyter Notebook的时候需要用首先更新pip,如下: 使用以下命令更新pip和安装Jupyter pip3 install --upgrade pi ...
- HDP 源码集
HDP 各个组件的源码(含历史各个版本) 分支 组件 标签 最后发版时间 地址 hadoop 2256 2020-12-21 17:44 https://gitee.com/piaolingzxh/h ...
- 使用 useSeoMeta 进行 SEO 配置
title: 使用 useSeoMeta 进行 SEO 配置 date: 2024/7/30 updated: 2024/7/30 author: cmdragon excerpt: 摘要:本文介绍了 ...
- Mysql将查询出的数值转换为中文显示case..when..then
我们经常需要在数据库导出文件,可是导出某些字段时不是中文含义其它同事分不清.可以通过case..when..then根据一一对应的关系将值转成中文,再进行导出方便大家查阅. 1.正常sql未处理之前查 ...
- 【FastDFS】环境搭建 01 跟踪器和存储节点
FastDFS:分布式文件系统 它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题. 特别适合以文件为载体的在线服务,如相册网站.视频网 ...
- Nvidia的Metropolis平台 —— AI监控解决方案和视频分析技术
相关: https://baijiahao.baidu.com/s?id=1566933142821989&wfr=spider&for=pc https://baijiahao.ba ...
- 树莓派3b+ 系统(Raspbian)环境搭建以及配置
多年前购入树莓派3b+板子一块,一直没时间弄,近期疫情假期在家翻出来打算鼓捣鼓捣. 1. 树莓派系统下载: 链接地址: https://www.raspberrypi.org/downloads ...
- 国内网友搭建的 Huggingface 镜像站 —— https://hf-mirror.com/
huggingface是什么这里不作解释. hugggingface的网站访问同Google一样,不能直接通过浏览器访问,需要科学的方式才可以,这里假设大家是知道如何访问Google的: huggin ...
- baselines算法库common/vec_env/dummy_vec_env.py模块分析
baselines算法库设计可以和多个并行环境进行交互,也就是并行采样,实现多进程并行采样的模块为subproc_vec_env.py,与此相对的只实现单个进程下多环境交互的模块即为本文所要讲的dum ...