最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得有什么特别大的用处,敲一敲才发现其实讲究还是比较多的。哈希冲突是常有的事,换一下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 字符串哈希的更多相关文章

  1. HDU2594 Simpsons’ Hidden Talents —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...

  2. hdu2594 Simpsons' Hidden Talents【next数组应用】

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. hdu2594 Simpsons’ Hidden Talents kmp

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...

  4. HDU2594 Simpsons’ Hidden Talents 【KMP】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  5. HDU2594——Simpsons’ Hidden Talents

    Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...

  6. 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 ...

  7. 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 ...

  8. hdu2594 Simpsons’ Hidden Talents

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...

  9. HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. 杭电ACM2085--核反应堆

    http://acm.hdu.edu.cn/showproblem.php?pid=2085 这也是一个简单的的递推. a[i][0] = 3*a[i-1][0]+2*a[i-1][1]; a[i][ ...

  2. Connected_Component Labelling(联通区域标记算法) C++实现

    // Connected-Component Labelling.cpp : 定义控制台应用程序的入口点.//如有是使用,请务必注明引用出处网站:http://www.cnblogs.com/Amat ...

  3. Netfilter深度解剖

         在网络上发现这个Netfilter写的很好的系列文章,为了便于后期寻找,特此标注下博客地址,感谢大大神提供.     ---------------------------分割线开始---- ...

  4. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

  5. VIM小技巧之文件名补全

    恩,这两天在看<简明Python教程>,那里面作者建议写代码的时候前面的注释写上文件名,写上调用的解释器,比如这样: 恩,然后我当然不可能每回新建一个文件,就要在开头写上一大串东西啊,vi ...

  6. Html.TextBoxFor三元判断

    @Html.TextBoxFor(item => item.DiscountOW,(Model.TripType == "单程" || (Model.TripType == ...

  7. JS对Json对象Distinct

    Json对象去重 今日有一个需求如下: 从数据库中取出数据源转化成json字符串绑定到隐藏域中,取出的json字符串如下: string data="[{"CompanyName& ...

  8. SVM入门

    前言: 又有很长的一段时间没有更新博客了,距离上次更新已经有两个月的时间了.其中一个很大的原因是,不知道写什么好-_-,最近一段时间看了看关于SVM(Support Vector Machine)的文 ...

  9. linux文件目录下各文件简介

    /bin:存放最常用命令: /boot:启动Linux的核心文件: /dev:设备文件: /etc:存放各种配置文件: /home:用户主目录: /lib:系统最基本的动态链接共享库: /mnt:一般 ...

  10. 终于成为博客员的一员了,这是我的第一篇博文,写一个关于ul li内容宽度的问题和解决方案

    第一次写博文,写一个刚才遇到的问题吧. 关于ul li文字长度不固定,一行显示多列.当指定宽度时,文字长度超过指定的li宽度时解决方案: 如下代码 <h4>发送对象(共10个会员)< ...