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.

InputThe 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. 
OutputFor 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的我mmp
网上的题解全是kmp
我觉得hash暴力也应该也行 实践证明 暴力hash是可以的
 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e4 + ;
const int seed = ;
ull HASH, s[maxn*], p[maxn*];
char a[maxn], b[maxn*], ans[maxn*];
void init() {
p[] = ;
for (int i = ; i < maxn ; i++)
p[i] = p[i - ] * seed;
}
int main() {
init();
int t;
scanf("%d", &t);
while(t--) {
scanf("%s%s", a, b );
int lena = strlen(a), lenb = strlen(b);
HASH = ;
for (int i = ; i < lena ; i++)
HASH = HASH * seed + a[i];
int top = ;
s[] = ;
int sum = ;
for (int i = ; i < lenb ; i++) {
ans[top++] = b[i];
s[top] = s[top - ] * seed + b[i];
if ( top >= lena && s[top] - s[top - lena]*p[lena] == HASH ) sum++;
}
printf("%d\n", sum);
}
return ;
}

外加一个kmp写法吧  毕竟多学点东西肯定没有错

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e4 + ;
int nxt[maxn], lena, lenb, ans;
char a[maxn], b[maxn * ];
void Get_nxt() {
int i = , j = - ;
nxt[] = -;
while(i < lena) {
if (j == - || a[i] == a[j] ) {
++i, ++j;
nxt[i] = j;
} else j = nxt[j];
}
}
void kmp() {
int i = , j = ;
Get_nxt();
while(i < lenb) {
if (j == - || b[i] == a[j]) {
++i, ++j;
if (j == lena) ans++;
} else j = nxt[j];
}
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
ans = ;
scanf("%s%s", a, b);
lena = strlen(a);
lenb = strlen(b);
kmp();
printf("%d\n", ans);
}
return ;
}

Oulipo HDU - 1686的更多相关文章

  1. Oulipo - HDU 1686 (KMP模板题)

    题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W.   分析:还是基础的KMP应用....................... 直接上代码. === ...

  2. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

  3. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  4. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  5. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  6. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  7. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  8. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  9. HDU 1686 Oulipo(KMP变形求子串出现数目(可重))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...

随机推荐

  1. python装饰器+递归+冒泡排序

    冒泡排序 li = [33, 2, 10, 1,23,23523,5123,4123,1,2,0] for k in range(1,len(li)): for i in range(len(li) ...

  2. POLYGON(动态规划)

    学校老师布置的一道动规的题目,要求下次上课前AC.周一一放学就回家写,调试了一会儿OK了.在这边记录一下解题的思路和过程,也作为第一篇随笔,就是随便之一写,您也就随便之一看.有问题望你指出,多多包涵. ...

  3. python2.7入门---变量类型&案例

      这篇文章呢,主要是用来记录python中的变量类型学习内容的.接下来就来看一下变量类型,那么什么是变量呢.变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间.基于变量的数据类型,解 ...

  4. Android使用butterknife注解出现nullPointerException解决

    1.下载butterknife加入到你的libs中,构建到你的项目中,此时还不能注解成功,必须进行2配置 2.选择你的项目右键---->properties----->java compi ...

  5. mysql字符串拼接,存储过程

    添加字段: alter table `user_movement_log`Add column GatewayId int not null default 0 AFTER `Regionid` (在 ...

  6. poorpool 的 考场 NOI Linux 配置

    把~/.bashrc里的force_color_prompt=yes前面那个#去掉,终端就有高亮啦qwq!(然后source一下 然后vim ~/.vimrc然后加入 (为什么不改/etc/vim/v ...

  7. Android TextView 单行文本的坑

    这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...

  8. 【多校联合】(HDU6045)Is Derek lying?

    分析 之前没有想到题目解法,看了题解才会,记录一下思考过程. 这条题目的实质是,在满足合法的情况下,有没有a和d的可行解?也就是说,不要仅仅附在表面的思考逻辑条件,而是要思考实际的数学表达. 转化为数 ...

  9. MySQL性能分析和优化-part 1

    MySQL性能优化 平时我们在使用MySQL的时候,怎么评估系统的运行状态,怎么快速定位系统瓶颈,又如何快速解决问题呢? 本文总结了多年来MySQL优化的经验,系统介绍MySQL优化的方法. OS性能 ...

  10. Leetcode 679.24点游戏

    24点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) ...