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 ...
随机推荐
- 如何在iview组件中使用jsx
最近选用的框架iview表单组件的render写法让人有点不习惯,尤其是在写比较复杂的逻辑的时候,还是感觉模板式的写法比较方便且可读性较强.而render函数除了支持配置写法外,还支持jsx的写法.由 ...
- java三元运算符
由?:符号表示的,具体的含义其实就和if-else结构的含义差不多,这种运算符会将某个条件作两种处理,如果满足条件的话就执行第一个结果,如果不满足的话就执行另外一个结果,例如: Int A,B,C; ...
- php备注
一.关于OOP 1.PHP目前不支持方法重载
- JQuery 判断指定ID是否存在
- centos配置网络
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE="eth0"BOOTPROTO=&qu ...
- Spring中的转换器:Converter
配置spring的配置文件: <bean id="conversionService" class="org.springframework.context.sup ...
- Python进阶内容(一)--- 高阶函数 High order function
0. 问题 # 本文将围绕这段代码进行Python中高阶函数相关内容的讲解 # 文中所有代码的兼容性要求为:Python 3.6,IPython 6.1.0 def addspam(fn): def ...
- 详细故障排除步骤:针对 Azure 中到 Windows VM 的远程桌面连接问题
本文提供详细的故障排除步骤,用于为基于 Windows 的 Azure 虚拟机诊断和修复复杂的远程桌面错误. Important 若要消除更常见的远程桌面错误,请务必先阅读远程桌面的基本故障排除文章, ...
- aspnetcore 认证相关类简要说明二
能过<aspnetcore 认证相关类简要说明一>我们已经了解如何将AuthenticationOptions注入到我们依赖注入系统.接下来,我们将了解一下IAuthenticationS ...
- java入门学习总结
1.jdk(java开发工具包)------> jre(java运行环境)------> jvm(java虚拟机) ------>应用(javac) -------> java ...