HDU题目

POJ题目

求目标串s中包含多少个模式串p

KMP算法,必须好好利用next数组,,

kmp解析)——可参考 海子的博客  KMP算法

//写法一:

#include<string>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h> using namespace std; void getNext(char *p,int *next)
{
int j,k;
int lenp=strlen(p);
next[]=-;
j=;
k=-;
while(j < lenp)
{
if(k==-||p[j]==p[k]) //匹配的情况下,p[j]==p[k]
{
++j;
++k; //若p[j]==p[k],则需要修正
if(p[j]==p[k])
next[j]=next[k];
else
next[j]=k;
}
else //p[j]!=p[k]
k=next[k];
}
} int KMPMatch(char *s,char *p)//返回s中包含p的个数
{
int next[]; //注意next数组的长度要和匹配的字符串长度一样
int lens=strlen(s),lenp=strlen(p);
int i,j,ans=;
i=;
j=;
getNext(p,next);
while( i < lens )
{
if(j==-||s[i]==p[j])
{
i++;
j++;
}
else
j=next[j]; //消除了指针i的回溯
if(j==lenp)
{
ans++;
j=next[j]; //再根据next数组从"前"一个找起,防超时,,
}
}
return ans;
} char str[],word[]; int main()
{ int n;
scanf("%d",&n);
while(n--)
{
scanf("%s%s",word,str);
int ans = KMPMatch(str,word);
printf("%d\n",ans);
}
return ;
}

//写法二:

#include <stdio.h>
#include <string.h> int lens,lenp;
char s[];
char p[];
int next[]; void get_next(int lenp, char *p, int *next)//求next数组
{
int i,j;
j=-;
next[]=-;
for (i=;i<lenp;i++)
{
while(j>= && p[j+]!=p[i]) j=next[j];
if (p[j+]==p[i]) j++;
next[i]=j;
}
} int kmp_num(char *s, char *p, int lens,int lenp) //返回s当中包含多少个p
{
int i,j,cnt;
cnt=;
j=-;
for (i=;i<lens;i++)
{
while(j>= && p[j+]!=s[i]) j=next[j];
if (p[j+]==s[i]) j++;
if (j==lenp-) cnt++;
}
return cnt;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",p);
scanf("%s",s);
lens=strlen(s);
lenp=strlen(p);
get_next(lenp,p,next);
printf("%d\n",kmp_num(s,p,lens,lenp));
}
return ;
}

HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)的更多相关文章

  1. poj 3461 字符串单串匹配--KMP或者字符串HASH

    http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...

  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(乌力波)

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

  4. 字符串匹配KMP算法详解

    1. 引言 以前看过很多次KMP算法,一直觉得很有用,但都没有搞明白,一方面是网上很少有比较详细的通俗易懂的讲解,另一方面也怪自己没有沉下心来研究.最近在leetcode上又遇见字符串匹配的题目,以此 ...

  5. 字符串匹配-KMP

    节选自 https://www.cnblogs.com/zhangtianq/p/5839909.html 字符串匹配 KMP O(m+n) O原来的暴力算法 当不匹配的时候 尽管之前文本串和模式串已 ...

  6. POJ 3461 Oulipo

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

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

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

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

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

  9. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  10. 字符串匹配KMP算法

    1. 字符串匹配的KMP算法 2. KMP算法详解 3. 从头到尾彻底理解KMP

随机推荐

  1. uva 11186 Circum Triangle<叉积>

    链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. kettle日志记录

    环境描述: 现在一个项目有很多个作业,需要知道每次跑批后哪些ktr跑成功,哪些失败了 问题解决: 下面是一个具体的操作流程 首先建立数据库表 CREATE TABLE test_1(id INT,NA ...

  3. Android线程---UI线程和非UI线程之间通信

        近期自学到了线程这一块,用了一上午的时间终于搞出来了主.子线程间的相互通信.当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message.我的主线程 ...

  4. 【HTML5】炫丽的时钟效果Canvas绘图与动画基础练习

    源自慕课网 效果如下: 全部代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  5. Python - DICT 字典排序 - OrderedDict

    官方地址: https://docs.python.org/2/library/collections.html#collections.OrderedDict >>> # regu ...

  6. Python实现nb(朴素贝叶斯)

    Python实现nb(朴素贝叶斯) 运行环境 Pyhton3 numpy科学计算模块 计算过程 st=>start: 开始 op1=>operation: 读入数据 op2=>ope ...

  7. 基础语法 swift

    强类型语言:每句代码可以不用分号分隔:大小写敏感: 变量声明: var a = 0 常量声明 let b = 3.14 常量不能+变量?a+b 类型标注 var s :String 打印 pringl ...

  8. Could not resolve placeholder

    使用spring的<context:property-placeholder location="/WEB-INF/redis.properties"/>读取prope ...

  9. RHEL 6.4中解决xx用户不在sudoers列表,此事将被报告的问题

    1.使用sudo service iptables status命令时报告没有权限: [tansheng@localhost ~]$ sudo service iptables status [sud ...

  10. 团队项目的NABC

    我们团队项目是做一个基于PC端的截屏软件,我觉得一个好的截屏软件需要具备磁性吸附的功能.当你需要对某个图像或者是其他的什么截屏的时候,精度比较高,不需要那些边框外的东西,磁性吸附就是在你选中的时候尽管 ...