题意:求b这个字符串在a中出现的次数

这里不用KMP,用hash

#include<cstdio>
#include<iostream>
#include<cstring>
#define P 29
#define N 1000010
using namespace std;
char a[N],b[N];int n1,n2,p,S,hash[N];
void get_p(){
p=;
for(int i=;i<=n1;i++)
p*=P;
}
void get_hash(){
S=;
for(int i=;i<=n1;i++)
S=S*P+a[i]-'a'+;
hash[]=;
for(int i=;i<=n2;i++)
hash[i]=hash[i-]*P+b[i]-'a'+;
}
int query(int x){
return hash[x+n1-]-hash[x-]*p;
}
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%s%s",a+,b+);
n1=strlen(a+);n2=strlen(b+);
get_p();get_hash();
int ans=;
for(int i=;i<=n2-n1+;i++)
if(query(i)==S)ans++;
printf("%d\n",ans);
}
return ;
}

Oulipo(poj 3461)的更多相关文章

  1. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  2. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  3. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  4. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  5. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  6. 并查集+关系的传递(poj 1182)

    题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...

  7. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  8. Collecting Bugs(POJ 2096)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3064   Accepted: 1505 ...

  9. Power string(poj 2406)

    题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len ...

随机推荐

  1. C++输入输出流

    一.C++输入输出流的含义 以前所用到的输入和输出,都是以终端为对象的,即从键盘输入数据,运行结果输出到显示器屏幕上.从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件.程序的输入指的 ...

  2. event相关

    event.button 事件属性可返回一个整数,指示当事件被触发时哪个鼠标按键被点击. event.keyCode  事件属性可返回一个整数,指示当事件被触发时哪个键盘按键被点击. <scri ...

  3. JSON字符串转JavaBean,net.sf.ezmorph.bean.MorphDynaBean cannot be cast to ……

    在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名.年龄等 public class Student implements java.io.Serializab ...

  4. 使用SQLPlus连接Oracle实例

    使用Windows徽标+R,打开运行对话框,输入cmd并回车. Microsoft Windows Microsoft Corporation.保留所有权利. C:\Users\user> 在C ...

  5. API23时权限不被许可

    In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Ins ...

  6. SVN钩子说明

    post-commit在提交完成,成功创建版本之后执行该钩子,提交已经完成,不可更改,因此本脚本的返回值被忽略. post-lock对文件进行加锁操作之后执行该脚本 post-revprop-chan ...

  7. VS2012创建UML项目

    1.选择建模工具 2.添加新建项 3.添加UML图或用例图 4.打开工具箱添加

  8. django中抽象基类的Foreignkey的定义

    class base(models.Model): user = models.ForeignKey(User) class Meta: abstract =True 以上是抽象基类的定义,只有一个公 ...

  9. eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式

    eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...

  10. Spring+SpringMVC+MyBatis+Maven 服务端XML配置

    项目目录结构 spring-mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...