题目大意

给定一个文本串和模式串,求模式串在文本串中出现的次数

题解

正宗KMP

代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
#define MAXN 10005
char T[MAXN*100],P[MAXN];
int f[MAXN];
void getFail()
{
int m=strlen(P);
f[0]=f[1]=0;
for(int i=1; i<m; i++)
{
int j=f[i];
while(j&&P[j]!=P[i]) j=f[j];
f[i+1]=P[j]==P[i]?j+1:0;
}
}
int find()
{
int n=strlen(T),m=strlen(P);
getFail();
int j=0,ans=0;
for(int i=0; i<n; i++)
{
while(j&&P[j]!=T[i]) j=f[j];
if(P[j]==T[i]) j++;
if(j==m) ans++;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",P,T);
printf("%d\n",find());
}
return 0;
}

POJ3461–Oulipo(KMP)的更多相关文章

  1. poj3461 Oulipo(KMP模板)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17795   Accepted: 7160 Descripti ...

  2. POJ3461 Oulipo KMP算法

    这个算法去年的这个时候就已经听过了,看毛片算法哈哈..不过理解它确实花了我很久的时间..以致于我一直很排斥字符串的学习,因为总觉得太难了,但是有些硬骨头还是要啃的,这个寒假就啃啃字符串还有一些别的东西 ...

  3. poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

  4. poj3461 Oulipo —— KMP

    题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...

  5. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  6. POJ-3461 Oulipo(KMP,模式串在主串中出现次数)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

  7. Oulipo (kmp)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26857   Accepted: 10709 Descript ...

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

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

  9. poj3461 Oulipo

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

随机推荐

  1. Linux_Struct file()结构体

    struct file结构体定义在/linux/include/linux/fs.h(Linux 2.6.11内核)中,其原型是:struct file {        /*         * f ...

  2. mysql 导出导入数据库中所有数据

    导出数据库所有数据 >mysqldump -uroot -proot -all hzgajzyz>e:/hzgajzyz.sql 导入数据库所有数据 >source e:/hzgaj ...

  3. python 删除文件和文件夹

    1.删除文件 '''删除文件 ''' def DeleteFile(strFileName): fileName = unicode(strFileName, "utf8") if ...

  4. System.out.println(对象)

    class Person { private String name; private int age; public String getName() { return this.name; } p ...

  5. DOM in Angular2

    <elementRef> import {ElementRef} from "@angular/core"; constructor(private element:  ...

  6. BIOS

    转自BIOS BIOS(Basic Input/Output System的缩写.中文:基本输入输出系统),在IBM PC兼容机上,是一种业界标准的固件接口.BIOS这个字眼是在1975第一次由CP/ ...

  7. jsp空页面导致的jvm heap溢出

    为了性能测试需要,写一个了简单的jsp页面: <%@ page contentType="text/html;charset=UTF-8" language="ja ...

  8. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程

    一. 1. 2.pizza-flow.xml <?xml version="1.0" encoding="UTF-8"?> <flow xml ...

  9. Delphi捕捉DLL执行所抛出的异常。

    先来说一下我如何写我的Dll文件的. 先看代码: 代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://w ...

  10. 打死也不换系统?笑谈过气的Windows XP

    http://tech.qq.com/a/20131012/007336.htm 按照IT领域的“安迪-比尔定律”:软件和游戏不断生成过户需求,硬件则通过技术创新来消化这些需求,这个过程会刺激用户在电 ...