1、题目大意:单字符串匹配问题

2、分析:经典KMP问题

存个模板QAQ

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char P[1000010];
char T[1000010];
int f[1000010];
inline void getfail(){
    int m = strlen(T);
    f[0] = f[1] = 0;
    for(int i = 1; i < m; i ++){
        int j = f[i];
        while(j && T[j] != T[i]) j = f[j];
        if(T[i] == T[j]) f[i + 1] = j + 1;
        else f[i + 1] = 0;
    }
    return;
}
int main(){
    int o;
    scanf("%d", &o);
    while(o --){
        int ans = 0;
        scanf("%s%s", T, P);
        getfail();
        int n = strlen(P), m = strlen(T);
        int j = 0;
        for(int i = 0; i < n; i ++){
            while(j && T[j] != P[i]) j = f[j];
            if(T[j] == P[i]) j ++;
            if(j == m){
                ans ++;
                j = f[j];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
} 

POJ3461——Oulipo的更多相关文章

  1. poj3461 Oulipo(KMP模板)

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

  2. poj3461 Oulipo

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

  3. 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中的出现位置或者是次数 这个出现的次数是可 ...

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

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

  5. POJ3461 Oulipo KMP算法

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

  6. POJ3461–Oulipo(KMP)

    题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...

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

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

  8. POJ3461 Oulipo 字符串

    正解:kmp/哈希 解题报告: 传送门! 这题其实就kmp板子,,,用来复习下kmp的太久没打了QAQ 所以kmp做法就不港了放个代码就是了QAQ #include<algorithm> ...

  9. poj3461 Oulipo —— KMP

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

随机推荐

  1. 安装和使用Linux花生壳(公网版)

    一.安装说明 1.下载相应的安装包,安装程序 2.运行程序.配置程序(默认使用/etc/phlinux.conf,如果不存在这个文件则自动进入交互配置) [root@localhost -]# phd ...

  2. 真机调试之android手机+chrome

    真机调试之android手机+chrome 虽然chrome上的移动设备模拟器很强大,但是在真机运行的时候,总会遇到一些小问题,这时就需要使用真机调试了. 第一步:准备一台android手机,并在手机 ...

  3. sql between and

    sql中的 a between 'a' and 'b' 基本上是代表 'a'>=a and 'b'<=a

  4. mybatis int 类型判断<if>

    如果数据类型是integer或者int,也就是数据类型的,在用<if>标签做动态语句的时候 不用判断是否为"''" <if test="sex != n ...

  5. Oracle开发常用函数与存储过程

    create or replace function Fuc_Get_AuthorName(RecID_In in varchar2, AdmID_In in varchar2) return var ...

  6. electron package can not find module xml2json

    问题 electron 打包好的应用找不到xml2json 但是开发环境npm start 运行正常 定位 node_modules没有包含在打的包中, 解决办法 --no-prune Be care ...

  7. Java——新IO 缓冲区与Buffer

    缓冲区和Buffer import java.nio.IntBuffer; //================================================= // File Na ...

  8. css教程

    网址:http://www.aa25.cn/layout/index.shtml

  9. 从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构

    Microsoft.AspNet.Identity简介 Microsoft.AspNet.Identity是微软在MVC 5.0中新引入的一种membership框架,和之前ASP.NET传统的mem ...

  10. Redis-cluster集群【第三篇】:redis主从

    Redis主从复制原理: 通过把这个RDB文件或AOF文件传给slave服务器,slave服务器重新加载RDB文件,来实现复制的功能! 复制的话:主服务器可以有多个从服务器!!!  不仅这样从服务器还 ...