题目链接:http://icpc.njust.edu.cn/Problem/Pku/3461/

代码如下:

 #include<cstdio>
#include<string.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x)
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x3f3f3f3f
#define maxn 1000010
int n,m,t;
ull Hash[maxn],h[maxn];
char a[maxn],b[maxn];
const int p=;
void init()
{
h[]=;
f(i,,maxn-)
{
h[i]=h[i-]*p;
}
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//std::ios::sync_with_stdio(false);
scan(t);
init();
while(t--)
{
scanf("%s%s",&a,&b);
int lena=strlen(a);
int lenb=strlen(b);
int ans=;
int hasha=a[]-'a',hashb;
f(i,,lena-)
{
hasha=hasha*p+a[i]-'a';
}
Hash[]=b[]-'a';
f(i,,lenb-)
{
Hash[i]=Hash[i-]*p+b[i]-'a';
}
f(i,,lenb-lena)
{
int l=i;
int r=l+lena-;
hashb=Hash[r]-Hash[l-]*h[lena];
if(hasha==hashb)ans++;
// dbg(ans);
}
pf("%d\n",ans);
}
}

POJ3461一道kmp题,字符串Hash也可的更多相关文章

  1. poj 3461 字符串单串匹配--KMP或者字符串HASH

    http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...

  2. Wannafly挑战赛11 D 题 字符串hash + 卡常

    题目链接 https://ac.nowcoder.com/acm/contest/73#question map与order_map https://blog.csdn.net/BillCYJ/art ...

  3. HDU 5763 Another Meaning dp+字符串hash || DP+KMP

    题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe   sub:hehe 那么,有**he.he** ...

  4. KMP替代算法——字符串Hash

    很久以前写的... 今天来谈谈一种用来替代KMP算法的奇葩算法--字符串Hash 例题:给你两个字符串p和s,求出p在s中出现的次数.(字符串长度小于等于1000000) 字符串的Hash 根据字面意 ...

  5. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  6. 2020牛客暑期多校训练营 第二场 A All with Pairs 字符串hash KMP

    LINK:All with Pairs 那天下午打这个东西的时候状态极差 推这个东西都推了1个多小时 (比赛是中午考试的我很困 没睡觉直接开肝果然不爽 一开始看错匹配的位置了 以为是\(1-l\)和\ ...

  7. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  8. Bazinga 字符串HASH 这题不能裸HASH 要优化 毒瘤题

    Ladies and gentlemen, please sit up straight. Don't tilt your head. I'm serious. For nn given string ...

  9. 字符串hash&&对字符串hash的理解

     对字符串hash的一些总结: 1,首先,我们在转化的时候,取底的时候一般是取131这些数,因为要避免不同的字符串对应相同的hash值这种情况的出现.如果卡精度的时候,我们可以采取双模数的方式尽量减少 ...

随机推荐

  1. 吴裕雄--天生自然 python数据分析:加纳卫生设施数据分析

    import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.rea ...

  2. 爬虫入门-使用python写简单爬虫

    从第一章到上一章为止,基本把python所有的基础点都已经包括了,我们有控制逻辑的关键字,有内置数据结构,有用于工程需要的函数和模块,又有了标准库和第三方库,可以写正规的程序了. python可以做非 ...

  3. 将Hexo网站托管到Coding.net

    只需要注册coding.net,然后建立一个名为用户名+coding.me的仓库即可,需要注意的是 coding.net的pages仓库只能有一个master分支 开始使用 Coding Pages官 ...

  4. 遗弃.Forsaken.2015.BluRay.720p.x264.DTS-beAst

    ◎译 名 遗弃/落日孤影(台)/赎罪◎片 名 Forsaken◎年 代 2015◎产 地 加拿大/法国/美国◎类 别 剧情/西部◎语 言 英语◎上映日期 2015-09-16(多伦多电影节)/2016 ...

  5. 修改 Cucumber HTML 报告

    后台服务是 JSON-RPC 风格的,所以 Scenario 都是这样的 Scenario: login successful When I set request body from "f ...

  6. Leetcode 141题 环形链表(Linked List Cycle) Java语言求解

    题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map ...

  7. mysql数据库笔记0

    mysql数据库笔记0 一次性添加多行数据 例如: INSERT INTO students (class_id, name, gender, score) VALUES (1, '大宝', 'M', ...

  8. Metaploit-永恒之蓝漏洞利用

    目录 Metaploit介绍 实验环境 漏洞利用过程 Metaploit介绍 本次测试主要是利用永恒之蓝漏洞对windows7进行控制利用,掌握Metaploit工具的使用,知道永恒之蓝的漏洞利用原理 ...

  9. LeetCode--链表3-经典问题

    LeetCode--链表3-经典问题 题1 反转链表 第一次代码超出时间限制 原因是,反转之后链表的尾部节点和头结点连上了 /** * Definition for singly-linked lis ...

  10. JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)

    5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...