题解:

简单kmp

然而strlen时间号费啊

代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
#define next ____next
int next[N],ans,len,lens,lenp,T;
char s1[N],s2[N];
void get_next(char a[])
{
int i=-,j=;
next[]=-;
while (j<lenp)
{
if (i==-||a[i]==a[j])next[++j]=++i;
else i=next[i];
}
}
void KMP(char a[],char b[])
{
int i=,j=;
while (i!=lens&&j!=lenp)
{
if (j==-||a[j]==b[i])i++,j++;
else j=next[j];
if (j==lenp)
{
ans++;
j=next[j];
}
}
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%s%s",&s1,&s2);
lens=strlen(s2);lenp=strlen(s1);
get_next(s2);
ans=;
KMP(s1,s2);
printf("%d\n",ans);
}
}

poj3461的更多相关文章

  1. 【poj3461】 Oulipo

    http://poj.org/problem?id=3461 (题目链接) 题意 求一个字符串在另一个字符串中出现的次数. Solution KMP裸题,太久没写过了,都忘记怎么求next数组了..水 ...

  2. [poj3461]Oulipo_KMP

    Oulipo poj-3461 题目大意:给你两个字符串s和p,问s中有多少个等于p的子串. 注释:$1\le strlen(p)\le 10^4\qquad1\le strlen(s)\le 10^ ...

  3. ACM -- 算法小结(四)KMP(POJ3461)

        KMP -- POJ3461解题报告 问题描述:给出字符串P和字符串T,问字符串P在字符串T中出现的次数 Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI ...

  4. (模板)poj3461(kmp模板题)

    题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...

  5. POJ3461 KMP 模板题

    最近忙着考研复习,所以刷题少了.. 数据结构昨天重新学习了一下KMP算法,今天自己试着写了写,问题还不少,不过KMP算法总归是理解了,以前看v_JULY_v的博客,一头雾水,现在终于懂了他为什么要在算 ...

  6. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...

  7. poj3461 Oulipo(KMP模板)

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

  8. POJ3461——Oulipo

    1.题目大意:单字符串匹配问题 2.分析:经典KMP问题 存个模板QAQ #include <cstdio> #include <cstdlib> #include <c ...

  9. 【POJ3461】Olipo

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

  10. poj3461 字符串匹配 熟悉kmp算法第一题

     题意:  计算一个字符串在另一个字符串中出现的次数. #include<cstdio> #include<cstring> #include<algorithm> ...

随机推荐

  1. python装饰器,其实就是对闭包的使用。

    装饰器 理解装饰器要先理解闭包(在闭包中引用函数,可参考上一篇通过例子来理解闭包). 在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 装饰器的实质就是对闭包的使用,原函数被 ...

  2. pip 更换国内镜像与记录

    更换pip源到国内镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/sim ...

  3. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼

    起初这样能短暂解决问题,后来发现每次机器重启了就还是有这样的错误,还是要执行SQL,很麻烦: show variables like '%time_zone%'; select now(); set ...

  4. 【Coursera】Security Introduction -Ninth Week(1)

    前言 Coursera 的 Internet History,Technology,and Security 进入最后一周的学习了,在这最后一周内,需要进行的内容是 public-key 公钥系统的讲 ...

  5. javascript面向对象的一些写法

    因为有闭包,能返回函数,所以针对于面向对象的封装,继承,多态三个特性实现,很舒服. 代码如下: <!DOCTYPE html> <html> <head> < ...

  6. BZOJ 1068 【SCOI2007】 压缩

    题目链接:压缩 区间动归水题.稍微有一点细节. 令\(f_{l,r}\)表示区间\([l,r]\)最短压缩长度,默认\(l\)位置之前有个\(M\).然后就枚举一下放不放\(R\),\(M\)放哪个位 ...

  7. Codeforces Beta Round #94 div2 D 优先队列

    B. String time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  8. python 进程队列

    #_*_coding:utf-8_*_ from multiprocessing import Process,Queue import os,time def f(q,n): q.put([n,'h ...

  9. Java中的hashcode方法

    一.hashCode方法的作用 对于包含容器类型的程序设计语言来说,基本上都会涉及到hashCode.在Java中也一样,hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,这样的散列 ...

  10. 解决MVC 时间序列化的方法

    1.全局处理 处理代码 publict static void SetSerializationJsonFormat(HttpConfiguration config) { // Web API co ...