HDU 1686 Oulipo (可重叠匹配 KMP)
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21428 Accepted Submission(s): 8324
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.
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.
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
3
0
#include<cstdio>
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
char moban[],wenben[];
int next1[];
int sum;
void getnext(char* s,int* next1,int m)
{
next1[]=;
next1[]=;
for(int i=;i<m;i++)
{
int j=next1[i];
while(j&&s[i]!=s[j])
j=next1[j];
if(s[i]==s[j])
next1[i+]=j+;
else
next1[i+]=;
}
}
void kmp(char* ss,char* s,int* next1,int n)
{
int m=strlen(s);
getnext(s,next1,m);
int j=;
for(int i=;i<n;i++)
{
while(j&&s[j]!=ss[i])
j=next1[j];
if(s[j]==ss[i])
j++;
if(j==m)
{
sum++;
//注意这里j不用复位为0,而是需要等于next[j],可重叠匹配匹配完之后需要跳到开始重叠的部分
j=next1[j];//!!!
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sum=;
scanf("%s",moban);
scanf("%s",wenben);
int L=strlen(wenben);
kmp(wenben,moban,next1,L);
printf("%d\n",sum);
}
return ;
}
HDU 1686 Oulipo (可重叠匹配 KMP)的更多相关文章
- HDU 1686 Oulipo(优化的KMP)
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- 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 ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- HDU 1686 Oulipo (KMP 可重叠)
题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...
- HDU 1686 Oulipo(KMP+计算匹配成功次数)
一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...
- hdu 1686 Oulipo 【KMP】(计算模式串匹配的次数——与已匹配的字串可以有交集)
题目链接:https://vjudge.net/contest/220679#problem/B 题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于100 ...
- HDU 1686 - Oulipo - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...
- hdu 1686 Oulipo kmp算法
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...
随机推荐
- Oracle数据库基本操作(二) —— 视图、序列、索引、同义词
一.视图(Views)与 同义词 1.视图:实际上是对查询结果集的封装,视图本身不存储任何数据,所有的数据都存放在原来的表中; 在逻辑上可以把视图看作是一张表 2.作用: 封装查询语句,简化复杂的查询 ...
- MySql:局域网和权限用户管理
MySql 5.6(XP)/5.7(win7) 添加用户和设置局域访问权限操作.请在 http://sourceforge.net/ 下载MySql Control Center(不是安装版本). ...
- sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code
MySQL存储过程: #插入一条返回值涂聚文注 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $ ...
- 拆系数FFT(任意模数FFT)
拆系数FFT 对于任意模数 \(mod\) 设\(m=\sqrt {mod}\) 把多项式\(A(x)\)和\(B(x)\)的系数都拆成\(a\times m+b\)的形式,时\(a, b\)都小于\ ...
- 2806 红与黑 个人博客:doubleq.win
个人博客:doubleq.win 2806 红与黑 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Descripti ...
- 地图的平移、缩放的实现(android版)
一.平移地图 移动地图的原理是利用手指在屏幕上拖动的距离,转换为在地图上距离,把地图坐标加上偏移的距离实现地图移动. 由于地图是绘制到Bitmap上的,所以地图移动和缩放的过程只要改变Bitmap的矩 ...
- Activity 四种launchMode
launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的 Activity实例,是否和其他Activity实例公用一个tas ...
- MySQL在线加字段实现原理
博客已转移到腾讯DBA博客 http://tencentdba.com/ 腾讯互娱内部维护了一个MySQL分支,基于官方5.5.24,实现了类似于Oracle 11g的快速加字段功能,这个分支我们内部 ...
- SQL Server ->> 性能调优案例之 -- 包含递归查询的视图导致整个查询语句性能下降
有个语句最近性能下降很厉害,原本1秒就可以查询完毕的事情现在居然需要3-4分钟. 首先我的做法是先快速找出导致整个语句下降的元凶.在这个例子里面查询语句有3个JOIN字句,我通过删除某一个JOIN节点 ...
- Sqlserver函数基础使用
函数基本功能: 转换工厂日期,将8点20之前的时间转化为前一天的时间. if exists (select * from sysobjects where xtype='fn' and name='F ...