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性能优化从入门到实战:13 内存篇:内存指标/工具总结、问题定位和调优
内存性能指标 系统内存指标 已用内存和剩余内存很容易理解,就是已经使用和还未使用的内存. 共享内存是通过 tmpfs 实现的,所以它的大小也就是 tmpfs 使用的内存大小.tmpfs 其实也是一种特 ...
- [sqlmap 源码阅读] heuristicCheckSqlInjection 探索式注入
上面是探索式注入时大致调用过程,注入 PAYLOAD 1.)("'(.((. , 数据库报错,通过报错信息获取网站数据库类型(kb.dbms),并将保存报错(lasterrorpage). ...
- jQuery ajax上传文件实例
jQuery ajax上传文件实例 <form id="form" enctype="multipart/form-data"><input ...
- Java浏览器弹出下载框,多个文件导出压缩包
项目里一直有这个功能,也一直没怎么注意,今天研究了一下 依据逻辑往下走: 首先是要下载的ajax的Java方法,只有返回值需要设定一下,其他的不用管: Map<String, Object> ...
- 关于DNS
一.什么是DNS DNS 是域名系统 (Domain Name System) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP ...
- 第五周作业—N42-虚怀若谷
一.查找/etc目录下大于1M且类型为普通文件的所有文件 [root@centos7 ~]# find /etc -type f -size +1M -exec ls -lh {} \; -r--r- ...
- @property属性装饰器
顾名思义,@property就是一个跟属性相关的装饰器, 使用了它之后,取值和赋值操作都变得简洁 from datetime import date, datetime class User: def ...
- CentOS下安装Chrome浏览器
1. 下载安装脚本, 在下载目录中,执行以下命令,将安装脚本下载到本地 wget https://intoli.com/install-google-chrome.sh 2.然后授予可执行权限 chm ...
- php array_chunk()函数 语法
php array_chunk()函数 语法 作用:把数组分割为新的数组块.dd马达参数 语法:array_chunk(array,size,preserve_key) 参数: 参数 描述 array ...
- 又联考了一场,感觉自己好菜啊,T2推出了公式但是不会逆元QAQ,难受啊!!!不过都确实是一道逆元的好题撒!
简单的玄学(random) 题目描述: 样例输入: 样例1: 3 2 样例2: 1 3 样例3: 4 3 样例输出: 样例1: 1 8 样例2: 1 1 样例3: 23 128 提示: 时间限制:10 ...