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. Python学习教程(learning Python)--3.3.2 Python的关系运算

    如果if的condition不用布尔表达式来做条件判断而采用关系表达式,实际上关系表达式运算的结果要么是True要么是False.下面我们先了解一些有关关系运算符的基础知识,如下表所示. 做个小程序测 ...

  2. android----Java DES加密算法工具类

    DESUtil类 public class DESUtil { private static byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (by ...

  3. hud 2586 How far away ?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Description There are n houses in ...

  4. Windows Phone播放视频流

    前言 MediaElement是Windows Phone中播放本地或者网络视频文件和音乐文件的常用控件,支持的格式可以从这里看.通过Play,Pause,Stop,Position方法或属性可以方便 ...

  5. EF - 批量插入

    比较一下下面两种方式的区别 1,每Add一次 就savechange() static void Main(string[] args) { //List<User> users= Fin ...

  6. 关于自定义的NavigationBar

    系统的NavigationBar局限太大,而且现在我要做的navigationBar需要四个按钮,一个Label,一个ImageView,所以不能用系统默认的. 刚刚咨询了一个高手,她的建议是,将系统 ...

  7. [转]Ubuntu 12.04开机自动挂载Windows分区

    [转]Ubuntu 12.04开机自动挂载Windows分区 http://www.cnblogs.com/A-Song/archive/2013/02/27/2935255.html 系统版本:Ub ...

  8. TI IPNC Web网页之流程分析

    流程 Appro IPNC使用的web服务器是boa. 请仔细理解下面这段话. boa这个web服务器是GUI界面和IPNC应用程序之间的通信的桥梁.它的责任是从web GUI中接收HTTP请求,并且 ...

  9. Oracle EBS中查询Profile的各种SQL【转载】

    1.List E-Business Suite Profile Option Values For All Levels SELECT p.profile_option_name SHORT_NAME ...

  10. (转载)HTML:模拟链接被按下,在新标签页打开页面,不使用window.open(可能被拦截)

    原文: http://www.cppblog.com/biao/archive/2010/08/21/124196.html 当按下一个按钮时,想打开一个新的标签页,可以使用window.open去实 ...