The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0

解题思路

基本上是 哈希和KMP的板子题,在这里主要讲下KMP:主要就是维护一个next数组。
先给个列子比如
0 0 1 2 3 4 5
A Z A Z A Z A 下面是标号,上面是next数组,**while(s[i]!=s[next[i-1]]&&next[i-1]>0) next[i-1]=next[next[i-1]-1]; **if(s[i]==s[next[i-1]]) next[i]=next[i-1]+1;else next[i]=0 **

0 1 2 3 4 5 6
这个有什么用那,别着急看这个例子

0 0 1 0 1 2 3 2 3 2 3
A Z A $ A Z A Z A Z A
0 1 2 3 4 5 6 7 8 9 10

我们让子串加上母串再求next数组就得到了这么的答案,等等我们发现了s[6]=3s[8]=3s[10]=3
正好是3个,答案也是3个(amazing),我讲一下next[7]是怎么求的,求next[7]我们要看s[7]是否等于s[next[6]]很明显s[7]=z,s[next[6]]=$,不成立,怎么办,看上面的式子next[6]=next[next[6]-1]=1,在比较s[next[6]]与next[7]是否相等,一看相等就加一next[7]=next[6]+1=2。自己用手模拟一下。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
string s1,s2;
vector<int> getnxt(string t)
{int len= t.size();vector<int> nxt(len);for(int i=1;i<len;i++){int j=nxt[i-1];while(t[i]!=t[j]&&j>0) j=nxt[j-1];//求next数组 if(t[i]==t[j]) j++;nxt[i]=j;}return nxt;
}int main()
{ios::sync_with_stdio(0);//关下同步不然会超时cin.tie(0);cout.tie(0); int t;cin>>t;while(t--){cin>>s1>>s2;int len1=s1.size();string s3=s1+'#'+s2;//这个构造一个新的字符串 vector<int> nxt=getnxt(s3);int len2=s3.size();int k=0;for(int i=len1+1;i<len2;i++){if(nxt[i]==len1)//枚举长度 {k++;}}printf("%d\n",k);   }return 0;
}

Oulipo(KMP哈希)相关推荐

  1. Oulipo (KMP算法)

    Oulipo (KMP算法) 题目链接:HDU-1686 题目: Oulipo Problem Description The French author Georges Perec (1936–19 ...

  2. POJ 3461 字符串匹配(KMP / 哈希(有推导))

    文章目录 1. 题目 1.1 题目链接 1.2 题目大意 2. Accepted代码 2.1 KMP解法 2.2 哈希法(有推导过程) 1. 题目 1.1 题目链接 http://poj.org/pr ...

  3. Oulipo[字符串哈希]

    题目 The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ...

  4. F - Oulipo(kmp经典模板题)!!!

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  5. HDU - 1686 Oulipo KMP

    Oulipo Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispari ...

  6. POJ - Oulipo(KMP)

    题目链接:http://poj.org/problem?id=3461 Time Limit: 1000MS Memory Limit: 65536K Description The French a ...

  7. Oulipo(kmp算法)

    题目意思就是找子串在一个长串中出现的次数.  因为数据较大,一般的做法都超时,看了看kmp算法,用这个算法做的.这个算法最难的就是求那个next数组吧. #include<iostream> ...

  8. POJ3461 Oulipo ——KMP算法——Pku3461

    建议大家学一学比较巧妙的KMP算法吧,很有意思.推荐个题目:POJ3167 Cow Patterns 题解我会发在本博里. 这个KMP就木有什么好说的了吧,大家找百度百科学一下就可以了~ CODE P ...

  9. HDU 1618 Oulipo KMP解决问题的方法

    鉴于两个字符串,寻找一个字符串的频率,另一个字符串出现. 原版的kmp另一个陷阱.以下凝视了,标不是踩着好,有加班一定几率,也有机会错误,根据不同的字符串可以是详细. 变化看起来像一个,kmp速度是非 ...

最新文章

  1. shell编程1到10求和_重磅|郑州市第四届中小学创意编程暨智能设计大赛初中组真题解析(下)...
  2. UA PHYS515 电磁理论I 麦克斯韦方程组基础3 麦克斯韦方程的势能形式
  3. RecycleView 与 Elevation
  4. java dump分析工具_Java 性能分析工具 (2):Java 内置监控工具
  5. 通过phoenix导入数据到hbase出错记录
  6. python 爬虫 微博 github_GitHub - berluo/weiboSpider-1: 新浪微博爬虫,用python爬取新浪微博数据...
  7. React09——使用脚手架编程
  8. 分享几个HIFI音乐下载网站
  9. 常见的图标库有哪些?
  10. 均值、方差、标准差、协方差详解及MATLAB实现
  11. 计算机控制/SLAM/ROS2云班课等详细说明2019-2020-2学期
  12. 关于 WARN conf.Configuration: bad conf file: element not property
  13. 基于安卓的校园二手跳蚤市场APP设计与实现.rar(项目源码+论文)
  14. Excel如何根据分组插入空行
  15. [codeforces1139C]Edgy Trees
  16. 机器人搭建和少儿编程的区别
  17. 用requests和pandas爬取中国福彩网官网 双色球 历年全部彩票数据
  18. ubuntu下lintel的安装配置
  19. 从此叫他马院士!马斯克入选美国工程院院士,张宏江博士入选外籍院士
  20. html5关于元素溢出相关属性6,1+x 证书 Web 前端开发初级实操考试(试卷 6 )

热门文章

  1. 玩转华为ENSP模拟器系列 | 同一VdPdNd实例场景下配置IPSec VdPdNd
  2. 高并发下的分布式缓存浅析
  3. 什么是网络端口?或许工作10年的人也很难100%理解
  4. js array formdata_这样写的 JS 代码看着就很舒服
  5. PMP备考总结 Part 4
  6. WIN/MAC使用exFat格式共用移动硬盘
  7. 杭州某B2B软件公司面试题
  8. sap 新建事务_sap 常用事务代码
  9. hyk-proxy 构建于GAE之上的高性能web代理
  10. 【正点原子FPGA连载】 第四章Vivado软件的安装和使用 摘自【正点原子】DFZU2EG/4EV MPSoC 之FPGA开发指南V1.0