HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)
HDU题目
求目标串s中包含多少个模式串p
KMP算法,必须好好利用next数组,,
//写法一:
#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)的更多相关文章
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
- 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 ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- 字符串匹配KMP算法详解
1. 引言 以前看过很多次KMP算法,一直觉得很有用,但都没有搞明白,一方面是网上很少有比较详细的通俗易懂的讲解,另一方面也怪自己没有沉下心来研究.最近在leetcode上又遇见字符串匹配的题目,以此 ...
- 字符串匹配-KMP
节选自 https://www.cnblogs.com/zhangtianq/p/5839909.html 字符串匹配 KMP O(m+n) O原来的暴力算法 当不匹配的时候 尽管之前文本串和模式串已 ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- 字符串匹配KMP算法
1. 字符串匹配的KMP算法 2. KMP算法详解 3. 从头到尾彻底理解KMP
随机推荐
- C语言接口的写法(以toyls命令为例)
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- .NET开源工作流RoadFlow-流程设计-流程步骤设置-数据设置
数据设置是控制在流程处理过程中,当前步骤的数据显示与编辑状态,控制当前步骤哪些字段为只读,隐藏或可编辑.需要配合表单设计器使用.
- oracle分区表(整理)
Oracle 表分区 早在8.0.5版本中,Oracle就将范围分区技术引入,现在分区功能已经越来越强大,包括支持扩展分区功能.Interval分区.外键分区.模拟列分区.以及分区建议器等.那么,分区 ...
- 编译原理之lex,yacc学习
写在前面的几句废话 最近在项目的过程中接触了lex 和 yacc,他们可以帮助我们来实现自己的领域语言.最典型的应用就是可以帮助我们来实现自定义测试脚本的执行器.但是,这里也有一个限制,就是测试脚本要 ...
- jquery绑定事件失效的情况(转)
原文地址:http://www.thinksaas.cn/group/topic/348453/ jQuery中文api地址:http://www.jquery123.com/api/ jQuery官 ...
- Windows Phone动画
从事Windows Phone开发已经有一段时间了,但是一直没有好好的静下心来梳理一下自己这段时间的知识,一是怕自己学问不到家,写不出那些大牛一般的高屋建瓴:二是以 前一直没有写博客的习惯:好了废话不 ...
- UISegmentedControl swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- Java Day 09
子父类的构造函数 在子类的构造函数中,第一行有一个默认的隐式语句:super() 子类的实例化过程:子类中所有的构造函数默认都会访问父类中的空参数的构造函数. 为什么子类实例化的时候要访问父类中的构造 ...
- struts1 和 struts2中Action什么时候实例化
精帖1:http://blog.csdn.net/lfsf802/article/details/7277013 精帖1:http://blog.csdn.net/wmj2003/article/de ...
- linux内核分析之fork()
从一个比较有意思的题开始说起,最近要找工作无意间看到一个关于unix/linux中fork()的面试题: #include<sys/types.h> #include<stdio.h ...