题意:求匹配串在文本中出现次数,KMP应用,理解了就OK了,每次匹配成功就累加次数,开始的时候超时,

由于在处理每次成功的时候让i=i-len2+1,相当于回溯了,后来一想,本次成功,相当于“失败”,i不动,

j需要用哪里来匹配?当然是next{j}!嘛(此处j=len2)。第一次真正敲kmp,完全根据自己理解敲出来的总控。

#include<iostream>   //用string 220ms,char *,scanf 110ms
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
int next[40000];
void get_next(char * s) //next[len]也求得了(有意义)。
{
int i=0,j=-1;
next[0]=-1;
int len=strlen(s);
while(i<len)
{
if(j==-1||s[i]==s[j])
{
i++;j++;
if(s[i]==s[j]) //此处只为文本的匹配,可以加快!
next[i]=next[j];
else
next[i]=j;
}
else
{
j=next[j];
}
}
}
int count=0;
void kmp(char * tx,char * w)
{
int i=-1,j=-1; //为了下面++
int len2=strlen(w);
int len1=strlen(tx);
while(i<len1)
{
if(j==-1||tx[i]==w[j])
{
i++;j++;
}
else
{
j=next[j];
}
if(j==len2) //一次匹配成功,
{
count++;
j=next[j]; //j对应位置。
}
}
}
char tx[1000008];char w[10010];
int main()
{
int ta;
cin>>ta;
while(ta--)
{
count=0;
scanf("%s\n",w);
scanf("%s",tx);
get_next(w);
kmp(tx,w);
printf("%d\n",count);
}
return 0;
}

POJ 3461 kmp 应用的更多相关文章

  1. POJ 3461 kmp

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40168   Accepted: 16135 Descript ...

  2. 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 ...

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

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

  4. POJ 3461 Oulipo

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

  5. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  6. (KMP)Oulipo -- poj --3461

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=92486#problem/B http://poj.org/problem?id=3461 ...

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

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

  8. POJ - 3461 (kmp)

    题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  9. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

随机推荐

  1. 数据库-SQL语法:LEFT JOIN

    LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.(补充:left join是一对多的关系,表里所有符合条件的记 ...

  2. Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合

    用JdbcTemplate的方法完成, 查询数据库表,把用户表sw_user所有数据以List<User>集合返回 在JdbcTemplateDemo类中增加查询返回所有对象集合的方法qu ...

  3. python mysql备份脚本

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: pyinnobackup.py #@time: 2018/12/11 1 ...

  4. GIMP中的新建Layer与更改Layer大小

    这边可以直接New Layer,新建一个Layer,还可以New from Visible,第二种是将当前的状态下图像复制出来. 改变Layer的大小,一般的方法两种: Crop to Selecti ...

  5. IE10无法识别setPrototypeOf属性问题

    项目遇到一个需求,React16.6.0兼容IE10浏览器 首先在IE浏览器打开,IE11可以支持,打开控制台切换到IE10,页面白屏,控制台报错. 控制台报错 vue2.0 兼容ie9及其以上 Ma ...

  6. python logging with yaml

    Recently, I was made a service which can provide a simple way to get best model. so, i spent lot of ...

  7. Scrapy爬取多层级网页内容的方式

    # -*- coding: utf-8 -*- import scrapy from Avv.items import AvvItem class AvSpider(scrapy.Spider): n ...

  8. \include\configs\mx6q_sabresd.h

    /* * Copyright (C) 2012 Freescale Semiconductor, Inc. * * Configuration settings for the MX6Q Sabre ...

  9. DFS:POJ1562-Oil Deposits(求连通块个数)

    Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Description The GeoSurvComp geologic survey com ...

  10. Python学习案例

    例1.求101到200之间所有的质数,并打印总数. 说明:除去1和它本身之外,不能被其他数整除,就是质数. #!/bin/python #-*- coding:utf-8 -*- #使用集合法 l = ...