2017-08-13 19:31:47

writer:pprp

对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了)

所以这道题就作为一个模板,为大家使用吧。

题目大意:给你一个子串P和一个主串S,求在主串中有多少个子串?

代码如下:(需要注意的点我都标记好了,两个函数可以直接用)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> using namespace std;
int ans;
const int maxn = ;
int next[maxn];
char S[maxn],P[maxn]; //构造next数组
void get_next()
{
int i = ;
int j = -; int lenp = strlen(P); //要用额外变量,如果写在while循环中就会TLE next[] = -; while(i < lenp)
{
if(j == - || P[i] == P[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
} //开始模式匹配
void kmp()
{
int i = ;
int j = ; //要用额外变量,如果写在while循环中就会TLE
int lens = strlen(S);
int lenp = strlen(P); get_next(); //这个构造不能忘记写 while(i < lens && j < lenp)
{
if(j == - || P[j] == S[i])
{
i++;
j++;
}
else
j = next[j];
if(j == lenp)
{
ans++;
j = next[j];
}
}
}
int main()
{
int cas;
cin >> cas; while(cas--)
{
ans = ;
memset(next,,sizeof(next));
scanf("%s%s",P,S);
kmp();
cout << ans << endl;
}
return ;
}

poj 3461 - Oulipo 经典kmp算法问题的更多相关文章

  1. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  2. POJ 3461 Oulipo(——KMP算法)

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

  3. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  4. poj 3461 Oulipo,裸kmp

    传送门 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32373   Accepted: 13093 Desc ...

  5. poj 3461 Oulipo(KMP模板题)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36903   Accepted: 14898 Descript ...

  6. POJ 3461 Oulipo(KMP裸题)

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

  7. poj 3461 Oulipo(kmp统计子串出现次数)

    题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...

  8. poj 3461 Oulipo(KMP)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49378   Accepted: 19617 Descript ...

  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. HBase简介及集群安装

    一.Hbase概述 Apache HBase™是Hadoop数据库,是一个分布式,可扩展的大数据存储. 当您需要对大数据进行随机,实时读/写访问时,请使用Apache HBase™.该项目的目标是托 ...

  2. alexnet,VGG,googlenet,resnet

    非常好的一篇:https://my.oschina.net/u/876354/blog/1637819 alexnet和VGG没什么特别的,VGG深一些. Deep learning 实际上是一种 f ...

  3. docker安装入门

    docker安装入门 https://blog.csdn.net/earbao/article/details/49683175

  4. 如何查看windows某个目录下所有文件/文件夹的大小?

    如何查看windows某个目录下所有文件/文件夹的大小? TreeSize Free绿色汉化版是一款硬盘空间管理工具,用树形描述出来,能够显示文件大小和实际占用空间数及浪费的空间等信息,让你做出相应的 ...

  5. 3D游戏引擎中常见的三维场景管理方法

    对于一个有很多物体的3D场景来说,渲染这个场景最简单的方式就是用一个List将这些物体进行存储,并送入GPU进行渲染.当然,这种做法在效率上来说是相当低下的,因为真正需要渲染的物体应该是视椎体内的物体 ...

  6. Subime3 快捷键

    实用快捷键 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+ ...

  7. Linux系统——Rsync数据同步工具

    Rsync的优点及缺点 优点:类似cp命令.scp命令,但rsync为增量复制工具 缺点:针对大文件,效率非常高(打包再比对),针对小文件,效率非常低. Rsync作用 (1)可使本地和远程两台主机之 ...

  8. webService 总结

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

  9. Java设计原则—单一职责原则(转)

    定义: 应该有且仅有一个原因引起类的变更. There should never be more than one reason for a class to change. 优点: 1.类的复杂性降 ...

  10. spark-sql做ETL时遇到的两个问题

    项目中使用spark-sql来作ETL,遇到两个问题,记录一下. 问题1: spark-sql –master yarn –hiveconf load_date=`date –d ..`  -e 'i ...