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. LeetCode_Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  2. cmd 运行jar文件

    将java工程打成jar包,但第三方jar包并没有包含在包中,当在命令行中运行jar包时,出现类找不到的异常, 在网上看到解决办法是将第三方jar包放到JDK的扩展类文件夹中(%JAVA_HOME%/ ...

  3. 001-hive是什么

    一.基本概念 官网含义:https://cwiki.apache.org/confluence/display/Hive/Home The Apache Hive™ data warehouse so ...

  4. html5 live stream

    一.传统的安防监控/流媒体音视频直播基本架构 A/V device 信号采集(yuv/rgb) ---> 转码(h264/265) ---> 网络推送(rtsp/rtmp/http/onv ...

  5. ambari rest api (三)

    1.获取指定主机指定组件的信息列表 http://ip:8080/api/v1/clusters/hdp_dev/hosts/hadoop003.edcs.org/host_components/DA ...

  6. 查看Oracle相关日志 ADRCI

    ADRCI 进去以后 show  home  

  7. Yii 2.x 和1.x区别以及yii2.0安装

    知乎上有个类似的问题:http://www.zhihu.com/question/22924271/answer/23085751 大致思路不会变,开发流程变化也不是很大.有变化的是1.yii2带入的 ...

  8. Xcode控制台命令

    命令 解释 break NUM 在指定的行上设置断点 bt 显示所有的调用栈帧,该命令可用来显示函数的调用顺序 clear 删除设置在特定源文件.特定行上的断点,其用法为:clear FILENAME ...

  9. 成员函数查找[条款24]---《C++必知必会》

    调用一个成员函数,涉及三个步骤:第一步,编译器查找函数的名字:第二部,从可用候选者中选择最佳匹配函数:第三步,检查是否具有访问该函数的权限. #include<iostream> usin ...

  10. Linux系统——shell脚本

    shell脚本编程 作用:通过命令行解析的方式,自动执行设定好的程序或命令代码.(若将脚本挂到定时任务中,就会自动在非工作时间里自动触发执行程序) Shell脚本文件以“.sh”结尾 规范的Shell ...