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/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
随机推荐
- Nacos 高级详解:提升你的开发和部署效率
Nacos 高级 一 .服务集群 需求 服务提供者搭建集群 服务调用者,依次显示集群中各服务的信息 搭建 修改服务提供方的controller,打印服务端端口号 package com.czxy.co ...
- 【Mybatis-Plus】使用QueryWrapper作为自定义SQL的条件参数
发现同事的自定义SQL写法是这样的 连表之后使用的条件是 ${ew.customSqlSegment} @Param声明的常量: /** * wrapper 类 */ String WRAPPER = ...
- 拜登开始在YouTube上打竞选广告了 —— 美国总统的竞选广告已经开始媒体投放了
哈哈哈,老拜登,跑到YouTube上打广告了,这个画面真的太难想象,如果美国有"椰树"广告,估计拜登能弄个泳装上去打广告.有时不得不佩服西方搞的这种全民选举,最后搞的就和看小品似的 ...
- from joblib.pool import MemmapingPool 报错
修改方法: 将 from joblib.pool import MemmappingPool 修改为: from joblib.pool import MemmappingPool === ...
- 再探 游戏 《 2048 》 —— AI方法—— 缘起、缘灭(8) —— 2021年9月SOTA的TDL算法——《Optimistic Temporal Difference Learning for 2048》——完结篇
<2048>游戏在线试玩地址: https://play2048.co/ 如何解决<2048>游戏源于外网的一个讨论帖子,而这个帖子则是讨论如何解决该游戏的最早开始,可谓是&q ...
- 链接池偶尔报错:HikariPool-1 - Connection is not available, request timed out after 39985ms.
1.背景 线上服务器偶尔报错如下: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.e ...
- manim边学边做--点
几何图形是manim中最重要的一类模块,manim内置了丰富的各类几何图形,本篇从最简单的点开始,逐个介绍manim中的几何模块. manim中点相关的模块主要有3个: Dot:通用的点 Labele ...
- C#数据结构与算法实战入门指南
前言 在编程领域,数据结构与算法是构建高效.可靠和可扩展软件系统的基石.它们对于提升程序性能.优化资源利用以及解决复杂问题具有至关重要的作用.今天大姚分享一些非常不错的C#数据结构与算法实战教程,希望 ...
- C语言基础- Hello World
第一个C语言程序 Hello World # include <stdio.h> //#关键标识符,表示用用头文件:include:引入都文件关键字 // stdio.h:系统标准输入.输 ...
- Ubuntu20.04安装CUDA和CUDNN
CUDA是GPU深度学习的运行库,那么cuDNN就是训练加速工具,两者要相互配合使用,所以一般机器学习需要训练引擎(tensorflow-gpu) + CUDA + cuDNN使用.想不安装cuDNN ...