HDU2594 Simpsons’ Hidden Talents 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得有什么特别大的用处,敲一敲才发现其实讲究还是比较多的。哈希冲突是常有的事,换一下mod,换一下进制数才有可能过,另外一种说法是用两个互质的量做hash,如果两个都相等的话那冲突就会少很多,这个倒没有做过多大的尝试,侥幸地过了一下这道题
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define maxn 50000
#define mod 40157
#define xx 133
using namespace std; char s[maxn + 50];
char t[maxn + 50]; int h1[maxn + 50];
int h2[maxn + 50];
int xpow[maxn+50]; int main()
{
xpow[0] = 1;
for (int i = 1; i <= maxn + 20; i++)
xpow[i] = xpow[i - 1] * xx%mod;
while (~scanf("%s%s", s + 1,t + 1))
{
h1[0] = h2[0] = 0;
int n = strlen(s+1), m = strlen(t+1);
for (int i = 1; i <= n; i++) h1[i] = (h1[i - 1] * xx%mod + s[i])%mod;
for (int i = 1; i <= m; i++) h2[i] = (h2[i - 1] * xx%mod + t[i])%mod;
int ans = 0;
int len = min(n, m);
for (int i = 1; i <= len; i++){
int lhs = (h1[i] - h1[0] * xpow[i] % mod + mod) % mod;
int rhs = (h2[m] - h2[m - i] * xpow[i]% mod + mod) % mod;
if (lhs == rhs) ans = i;
}
if (!ans) puts("0");
else{
for (int i = 1; i <= ans; i++){
printf("%c", s[i]);
}
printf(" %d\n", ans);
}
}
return 0;
}
HDU2594 Simpsons’ Hidden Talents 字符串哈希的更多相关文章
- HDU2594 Simpsons’ Hidden Talents —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...
- hdu2594 Simpsons' Hidden Talents【next数组应用】
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu2594 Simpsons’ Hidden Talents kmp
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU2594 Simpsons’ Hidden Talents 【KMP】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU2594——Simpsons’ Hidden Talents
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
- hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.Marge ...
- kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...
- hdu2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- 济南学习 Day 4 T2 am
LYK 与实验室(lab)Time Limit:5000ms Memory Limit:64MB题目描述LYK 在一幢大楼里,这幢大楼共有 n 层,LYK 初始时在第 a 层上.这幢大楼有一个秘密实验 ...
- [翻译.每月一译.每日一段]Exploring Fonts with DirectWrite and Modern C++
Windows with C++ Exploring Fonts with DirectWrite and Modern C++ Kenny Kerr DirectWrite is an incred ...
- 【转】使用Memcached提高.NET应用程序的性能
在应用程序运行的过程中总会有一些经常需要访问并且变化不频繁的数据,如果每次获取这些数据都需要从数据库或者外部文件系统中去读取,性能肯定会受到影响,所以通常的做法就是将这部分数据缓存起来,只要数据没有发 ...
- SignalR 2.0 系列: 开始使用SignalR 2.0
这是微软官方SignalR 2.0教程Getting Started with ASP.NET SignalR 2.0系列的翻译,这里是第四篇:开始使用SignalR 2.0 原文:Getting S ...
- API地图坐标转化(批量转换坐标)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 原始套接字的简单tcp包嗅探
原始套接字 sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_TCP); while(1) { data_size = recvfrom(sock_raw ...
- mysql 连接语句
在 SELECT 语句中,如果 FROM 子句引用了多个表源或视图,可以使用 JOIN 指示指定的联接操作应在指定的表源或视图之间执行. 一.交叉联接:CROSS JOIN 交叉联接将执行一个叉积(迪 ...
- iOS开发应用学习笔记
一.iOS应用设计 1. 参考资料: 解读iPhone平台的一些优秀设计思路 iPhone App的特点及基本设计方法 Mobile UI design and Developer 2. 用户对iPh ...
- Java对象的序列化与反序列化
序列化与反序列化 序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程.一般将一个对象存储至一个储存媒介,例如档案或是记亿体缓冲等.在网络传输过程中,可以是字节或是 ...
- 微软职位内部推荐-SDE2 (Windows - Power)
微软近期Open的职位: SDE2 (Windows - Power) Windows Partner Enablement team in Operating System Group is loo ...