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 ...
随机推荐
- centos7卸载YUM后重装过程 -bash: yum: command not found / -bash: yum: 未找到命令
[root@localhost ~]# rpm -qa |grep yum yum-3.4.3-158.el7.centos.noarch yum-plugin-fastestmirror-1.1.3 ...
- Rsync+sersync 数据同步指南
(1):sersync 可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或 某一个目录的名字: (2):rsync 在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变 ...
- Taro -- 微信小程序wxParse达到html转换wxml
Taro微信小程序可以用wxParse来达到html转换wxml的效果:https://github.com/NervJS/taro-components-test/blob/master/src/p ...
- php函数漏洞
1.ereg — 正则表达式匹配 此函数遇 %00 截断. <?php $a = $_GET['pwd']; var_dump(ereg ("^[0-9]+$", $a)); ...
- 01.helloworld--标签
"""参考网站:http://python.cocos2d.org/doc/programming_guide/index.html""" ...
- Apache HttpClient之fluent API的使用
该方法为Apache HttpClient 4.5以上的版本支持,在官网有明确的说明. 对比以前的方式,其优点是代码更简洁,同时为线程安全的.仅举一个最简单的post栗子 JAR包信息: <de ...
- Linux启动redis提示 /var/run/redis_6379.pid exists, process is already running or crashed
执行启动命令:service redisd start 提示信息:/var/run/redis_6379.pid exists, process is already running or crash ...
- php prev()函数 语法
php prev()函数 语法 作用:将内部指针指向数组中的上一个元素,并输出.直线电机选型 语法:prev(array) 参数: 参数 描述 array 必需.指定需要操作的数组. 说明:如果数组包 ...
- php array_chunk()函数 语法
php array_chunk()函数 语法 作用:把数组分割为新的数组块.dd马达参数 语法:array_chunk(array,size,preserve_key) 参数: 参数 描述 array ...
- 使用PHPExcel操作Excel用法实例分析
本文实例分析了使用PHPExcel操作Excel用法.分享给大家供大家参考.具体分析如下: PHPExcel下载地址:http://www.codeplex.com/PHPExcel http://w ...