题意:主串中能找到几个模式串

思路:超详细解释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)题解的更多相关文章

  1. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  2. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  3. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  4. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  5. hdu 1686 Oulipo (kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  6. HDU 1686 Oulipo (KMP 可重叠)

    题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...

  7. 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp

    HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...

  8. HDU 1686 Oulipo kmp裸题

    kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...

  9. 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 ...

随机推荐

  1. hammerjs wabapp h5 触屏手势一网打尽

    hammerjs官网    http://hammerjs.github.io/ 学习文章1 http://www.cnblogs.com/vajoy/p/4011723.html 学习文章2 htt ...

  2. (2.17)Mysql之SQL基础——日期函数

    关键词:mysql时间函数,mysql日期函数 [1]curdate():返回当前日期(2019-03-06),curdate()+0 返回(20190306) [2]curtime():返回当前时间 ...

  3. gh-ost安装

    下载 : https://github.com/github/gh-ost/releases/tag/v1.0.28 先安装Go语言: sudo yum install golang 将gh-ost文 ...

  4. 003-Nginx 设置Header 获取真实IP

    1.X-Forwarded-For的定义: X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项.它 ...

  5. requesMapping注解

    java类 package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; impor ...

  6. [LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  7. RMAN备份与恢复实践(转)

    1   RMAN备份与恢复实践 1.1  备份 1.1.1 对数据库进行全备 使用backup database命令执行备份 RMAN> BACKUP DATABASE; 执行上述命令后将对目标 ...

  8. 非线性方程(组):MATLAB内置函数 solve, vpasolve, fsolve, fzero, roots [MATLAB]

    MATLAB函数 solve, vpasolve, fsolve, fzero, roots 功能和信息概览 求解函数 多项式型 非多项式型 一维 高维 符号 数值 算法 solve 支持,得到全部符 ...

  9. linux下操作iso文件的两个shell程序

    记得这还是当初玩cdlinux时弄的,当初应该是由于windows下的Ultraiso对cdlinux的镜像修改后导致镜像无法引导,所以就使用linux下的命令进行操作 这应该是挂载iso文件的命令: ...

  10. Linux root用户下强制静音的问题

    解决方法 pulseaudio --start --log-target=syslog suorce /etc/profile