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/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
随机推荐
- 关于使用c++制作蓝牙连接,Windows版本
1 #define _CRT_SECURE_NO_WARNINGS 2 #pragma warning(disable : 4995) 3 #include <iostream> 4 #i ...
- 自己在本地搭建 git 版本仓库服务器
请确保你安装了 git 的图形化工具和 git 软件 首先先创建一个目录作为你的项目工程目录,比如 e:/gitTest 其次右键 git init. 然后指定一个 git 服务器目录,例如:e:/g ...
- RHCA rh442 005 (NICE FIFO RR) 资源强占与分配 cpuset
cgroup 容器 控制服务访问 limits 控制用户 进程管理 [root@servera ~]# ps -aux | more USER PID %CPU %MEM VSZ RSS TTY ST ...
- RPA美团外卖商家中心批量发送消息
美团外卖商家中心批量发送消息,首先我们需要确定给谁发,发送什么内容 给谁发:可以传入美团用户名.美团订单号.美团将通过此条件进行搜索进入会话框 发送什么内容:批量发送信息给不同的用户,比如给不同的订单 ...
- 【C】Re03
一.变量 变量存储了两个东西: 1.内存空间地址 2.内存空间存放的值 本质是给内存地址起了一个别名,允许我们通过别名对内存进行访问 void variable01() { int a = 100; ...
- 暑假自学Java进度总结04
一.今日所学: 1.下载并使用idea开发工具 1>了解idea的发展历史 2>尝试用idea编写代码 3>学习idea中的项目和模块操作 2.学习赋值运算符 加后赋值:" ...
- 【转载】 Parallel Computing in Python using mpi4py
原地址: https://research.computing.yale.edu/sites/default/files/files/mpi4py.pdf ====================== ...
- mujoco_py 运行example报错:ERROR: GLEW initalization error: Missing GL version ——— 解决方法
mujoco的安装与mujoco_py的安装参见: https://www.cnblogs.com/devilmaycry812839668/p/16004320.html mujoco_py安装成功 ...
- 后端开发学习敏捷需求-->干系人分析与识别
干系人分析与识别 5W1H 干系人分析与识别 1. 干系人是什么 直接或者间接影响专题,以及被专题影响的人和组织,用户也是属于干系人,是产品直接或者间接的使用者 又叫利益相关者,指积极参与专题或者在专 ...
- 关于Arrays.asList返回List无法新增和删除?
关于Arrays.asList返回的List无法新增和删除? 这个是在写项目的时候发现的,然后就分析了一下源码,得其内部原理 复现代码示例: public class ArraysAsList { p ...