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. 装黑苹果的那些事儿(以ThinkpadE540为例)

    苹果系统,有着比window更好的安全性和方便性,更重要的事,没有MAC系统环境,进行iOS开发,是很麻烦的,对新手来说,是很懊恼的一件事.但是白苹果像件奢侈品,吾等常人,很难有经济消费.如是黑苹果是 ...

  2. Java学习中,常用的命令管理(Java 学习中的小记录)

    Java学习中,常用的命令管理      作者:王可利(Star·星星) 一.Window中常见的dos命令 在哪里操作dos命令: Win7 ---> 开始  ---->所有程序---& ...

  3. 每日一练--C语言--串

    目标 实现串的经典模式匹配算法与KMP算法. 简述 自定义串结构: 串采用定长顺序存储结构,串从下标1开始存储,0下标存储串的实际长度: 匹配成功返回匹配位置,匹配失败返回0. #include &l ...

  4. ThinkPHP3.2.2中开启REWRITE模式

    1. 在项目配置文件(\Application\Common\Conf\config.php)中配置URL模式 <?php return array( //URL模式 , ); 2. 在Thin ...

  5. SQL1092N The requested command or operation failed because the user ID does not have the authority to perform the requested command or operation.

    1.前一天安装号db2后,做了如下处理: ************************************************************ 修改 /etc/sudoers 文件 ...

  6. javascript 关于Date 时间类型 处理方法

    上一篇博客中和大家分享了关于 字符串转时间类型 这一篇顺便整理下 javascript 中 Date 类型的一些方法 var time = new Date(); var year=time.getY ...

  7. 手动书写小代码-foreach实现机制

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  8. 部署ghost博客

    wget https://ghost.org/zip/ghost-0.6.4.zip npm install --production NODE_ENV=production npm start &g ...

  9. Swift弹窗

    在一个ViewController中使用以下代码: let alertController = UIAlertController(title: "Game Set", messa ...

  10. Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null

    在开发Ext 项目中如果遇到 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null 这个错误,检查下renderT ...