kmp(多次可重叠匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1686
Oulipo
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.
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.
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.
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
3
0
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = ;
int c[N] ;
int n ;
//int p[220009] ;
char a[] ;
char b[]; void getnext(char *b , int *next)
{
int len = strlen(b);
int j = , k = - ;
next[] = - ;
while(j < len)//查找多次且可重叠时len不能减一,因为该单词的末尾加一的next也需要被下一次查询用到。
{
if(k == - || b[k] == b[j])
{
k++;
j++;
// 下面nest数组的优化
if(b[k] != b[j])
next[j] = k ;
else
next[j] = next[k];
}
else
{
k = next[k];
}
}
} int main()
{
int n ;
scanf("%d" , &n);
while(n--)
{
int next[];
scanf("%s" , b);
scanf("%s" , a);
int lena = strlen(a) , lenb = strlen(b);
getnext(b, next);
int i = , j = ;
int ans = ;
while(i < lena)
{
if(j == - || a[i] == b[j])
{
i++ ;
j++ ;
}
else
{
j = next[j];
}
if(j == lenb)
{
ans++;
j = next[j] ;
}
}
printf("%d\n" , ans);
} return ;
}
kmp(多次可重叠匹配)的更多相关文章
- HDU 1686 Oulipo (可重叠匹配 KMP)
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)
Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...
- Oulipo POJ - 3461(kmp,求重叠匹配个数)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- kmp(多次无重叠匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2087 剪花布条 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...
- KMP算法 (字符串的匹配)
视频参考 对于正常的字符串模式匹配,主串长度为m,子串为n,时间复杂度会到达O(m*n),而如果用KMP算法,复杂度将会减少线型时间O(m+n). 设主串为ptr="ababaaababaa ...
- kmp匹配详解
字符串算法都是毒瘤的 一.kmp算法的用处 在文本串中查找模式串的位置,数量 文本串:要在这个字符串查找模式串 模式串:在文本串中查找的字符串 全是废话 二.kmp算法的思想 话说kmp好像是3个发明 ...
- HDU1841——KMP算法
这个题..需要对KMP的模板理解的比较透彻,以前我也只是会套模板..后来才知道..之会套模板是不行的..如果不能把握模板的每一个细节`,至少能搞清楚模板的每一个模块大体是什么意思.. 题意是给出两个串 ...
- HDU 2087 剪花布条(模式串在主串中出现的次数主串中子串不可重叠)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题意:求模式串在主串中出现的次数,与模式串匹配的子串之间不可重叠. 思路:用kmp算法解决,在匹 ...
- KMP(构建next数组)
字符串匹配算法KMP, 核心思想是尽可能利用已经匹配的结果, 跳过尽可能多的不需要匹配的情况 重点和难点都在next数组的建立上 1. KMP算法的next数组求解 以模式串 a b a c a b ...
随机推荐
- Linux shell中自动完成登录
在写shell脚本时,需要登录到不同的服务器上执行相关命令,在未建立信任之前如何批量操作. 1.ssh 首次登录服务器时会提示RSA key fingerprint输入yes/no,可以通过下面的方法 ...
- Linux和VMware
1.1 Linux操作系统简介 是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位和64位硬件.Lin ...
- 【leetcode】1033. Moving Stones Until Consecutive
题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...
- web前后端分离漏洞分析防御
web前后端分离漏洞分析防御 漏洞分析,主要漏洞有 一.跨站脚本攻击XSS 程序 + 数据 = 结果:攻击后,数据夹杂一部分程序(执行代码),导致结果改变: 1.XSS攻击注入点 (a):HTML节点 ...
- Keyguard分析
从Android 6.0开始,位于frameworks/bases/packages/Keyguard的Keyguard开始被编译为一个jar包,被SystemUI静态导入,相当于SystemUI ...
- iOS取消按钮点击时的动画效果
当存在图片或者背景图片时,点击图片往往意味着高亮,但是高亮的同时往往还伴随着按钮按下的动画效果,取消这种动画效果只需两步 Btn.adjustImageWhenHighlighted =NO; [Bt ...
- OC + RAC(七) RACSubject和RACSignal的区别
-(void)_test8{ /// RACSubject继承自RACSignal 但是RACSubject和RACSignal的区别? //1能接收1,2 //但是2只能接收2 RACSubject ...
- c# 生成文件目录树
class Program { //遍历目录名含有M00到M11的目录 //生成文件目录树(去除文件名中含有scc\Designer\designer\resx的文件) //生成的文件保存在D:\\a ...
- SQL Server性能调优--索引
序言 索引的概念 索引是什么 数据库中的索引类似于一本书的目录,在一本书中使用目录可以快速找到你想要的信息,而不需要读完全书.在数据库中,数据库程序使用索引可以快速查询到表中的数据,而不必扫描整个表. ...
- 带有lazy标记的线段树
#include<bits/stdc++.h> using namespace std; ]; struct st{ int l,r,val,add; }tr[]; void build( ...