HDU 1686 Oulipo(KMP)题解
题意:主串中能找到几个模式串
思路:超详细解释KMP
KMP:针对这个代码,解释一下Fail数组的含义:T为主串,P为模式串,Fail代表失配值,即当P[j] != T[i]时,j要指向的位置为Fail[j],当Fail为-1时表示i指针后移。如果使用这个代码,Fail[j]的值的含义为P[0]…P[j-1]这个子串的前后缀匹配度,但是下面代码的不是!!!
代码:
#include<iostream>
#include<algorithm>
const int N = 1000000+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int fail[N];
char p[N],t[N];
void getFail(){
fail[0] = -1;
int j = 0,k = -1;
int len = strlen(p);
while(j < len){
if(k == -1 || p[j] == p[k]){
if(p[++j] == p[++k]) //两个字符相等时跳过
fail[j] = fail[k];
else
fail[j] = k;
}
else{
k = fail[k];
}
}
}
int KMP(){
int num = 0;
getFail();
int i = 0,j = 0; //主串位置和模式串位置
int lent = strlen(t),lenp = strlen(p);
while(i < lent){
if(j == -1 || t[i] == p[j]){ //当j为-1时要移动i,j归零
i++;
j++;
if(j == lenp) num++;
}
else{
j = fail[j]; //失配,j回到指定位置
}
}
return num;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s%s",p,t);
int ans = KMP();
printf("%d\n",ans);
}
return 0;
}
HDU 1686 Oulipo(KMP)题解的更多相关文章
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- 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 ...
- hdu 1686 Oulipo (kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- HDU 1686 Oulipo (KMP 可重叠)
题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...
- 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp
HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...
- HDU 1686 Oulipo kmp裸题
kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...
- 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 ...
随机推荐
- idea 创建的maven+spring+mybatis项目整合 报错无法创建bean
报错如下: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with n ...
- Unity优化----drawcall系列
本文由博主(国宝大熊猫)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/5151951.html 知识普及: *Drawcall影响的是CPU的效率.因为d ...
- /etc/rc.d/rc.local 自定义开机启动程序
/etc/rc.d/rc.local 用于用户自定义开机启动程序,可以往里写开机要执行的命令或脚本,线上的配置如下: [root@localhost ~]$ cat /etc/rc.d/rc.loca ...
- HashMap(不是线程安全)与ConcurrentHashMap(线程安全)
HashMap不是线程安全的 ConcurrentHashMap是线程安全的 从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. ...
- 64位win10+cuda8.0+vs2013+cuDNN V5下Caffe的编译安装教程并配置matlab2014a 接口
一.需要安装的软件 1)vs2013,我是在http://www.52pojie.cn/thread-492326-1-1.html这个网址安装的.我之前用的是vs2012,按照网上的配置教程会爆各种 ...
- iphone传照片还是用QQ比较好
之前设置相机格式为高效,通过用91助手的苹果助手导出来文件格式为HEIC,window下没法打开.网上介绍用微信传,发现传的是缩略图,不清楚.最后选择通过qq传比较高清.注意,如果从相册中进行选择的话 ...
- 百度编辑器UEditor源码模式下过滤div/style等html标签
UEditor在html代码模式下,当输入带有<div style="">.<iframe>这类带有html标签的内容时,切换为编辑器模式后,会发现输入的内 ...
- js 俄罗斯方块源码,简单易懂
1.自己引入jquery <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- python -- 解决If using all scalar values, you must pass an index问题
[问题描述] 在将dict转为DataFrame时会报错:If using all scalar values, you must pass an index 例如: summary = pd.Dat ...
- jquery 删除table行,该如何解决
query 删除table行< table > < tbody > < tr > < td > 这行原来就有 </ td > < ...