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 ...
随机推荐
- There is no Action mapped for namespace [/] and action name [TestAction] ass
1.修改action的name值 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...
- CentOS7搭建Hadoop2.8.0集群及基础操作与测试
环境说明 示例环境 主机名 IP 角色 系统版本 数据目录 Hadoop版本 master 192.168.174.200 nameNode CentOS Linux release 7.4.1708 ...
- 实现 unity MonoBehaviour API5.4 的消息
顺序(第一次执行.忽略循环) 方法 说明 Editor 1 void Reset() 重置为默认值 ------------------------------------------------ ...
- MySQL的删除语句
虽然现在数据库空间越来越大,但处理数据时候还是有要删除的时候,以下整理了一些最常用的删除语句. 分成两种 一个是删除指定数据,另一个删除所有数据. 一.删除指定数据 DELETE FROM 表名 WH ...
- java selenium常用API汇总
(WebElement.iFrame.select.alert.浏览器窗口.事件.js) 一 WebElement相关方法 1.点击操作 WebElement button = driver. ...
- Spring---MongoDB
1.MongoDB概述 1.1.NoSQL数据库 1.1.1.NoSQL的主要特点: 不使用SQL语言 作为查询条件: 数据存储 也不是固定的表.字段: 1.1.2.NoSQL数据库 主要有 ...
- RAC搭建---自己做
一.本地磁盘是指你本身加上去的磁盘,只能本机使用的.共享磁盘是指可以多台机器同时读取写入.你做RAC就要用到共享存储: 二.ORC分区一般1G*3 数据分区5G*3 ,FRA分区一般5G*3 这 ...
- navicat的使用(测试库和正式库同步)以及用plsql改表字段属性
说明:数据库的操作,除了查询,最好先做好备份,比如数据同步.更新.修改或删除之类的: netstat -antp 查看mysql端口 firewall -cmd --list-all 查看防 ...
- NOIp 基础数论知识点总结
推荐阅读 NOIp 数学知识点总结: https://www.cnblogs.com/greyqz/p/maths.html Basic 常用素数表:https://www.cnblogs.com/g ...
- 使用CFStringTransform将汉字转换为拼音
之前做通讯录相关的一些App时,有一个比较常用的算法是将汉字转换成拼音.当时采用的做法是:将各个拼音段的首个汉字(按Unicode排序)做成两个数组,一个数组存拼音,另一个数组存拼音对应首个汉字的Un ...