最近忙着考研复习,所以刷题少了。。

数据结构昨天重新学习了一下KMP算法,今天自己试着写了写,问题还不少,不过KMP算法总归是理解了,以前看v_JULY_v的博客,一头雾水,现在终于懂了他为什么要在算完next[]之后,所有值都减一了。。。。。。

为了顺便复习下C语言,就没有用C++写,而且写出来比较丑。。

 #include <stdio.h>
#include <string.h>
#define LEN 100
void getNext(int *next, char *pattern)
{
int len = strlen(pattern);
next[] = -;
int i = , j = -;
while(i < len)
{
if(pattern[i] == pattern[j] || j == -)
{
++i, ++j;
next[i] = j;
if(patter[i] == pattern[j])
next[i] = next[j];
}
else
j = next[j];
}
} int KMP(char *str, char *pattern, int *next)
{
getNext(next, pattern);
int len1 = strlen(str), len2 = strlen(pattern);
int i = , j = ;
while(i < len1 && j < len2)
{
if(str[i] == pattern[j] || j == -)
{
i++, j++;
}else{
j = next[j];
}
}
if(j == len2)
return i-j;
return -;
}
int main()
{
int next[LEN];
char str[LEN], pattern[LEN];
int n, index;
scanf("%d", &n);
while(n--)
{
scanf("%s%s", str,pattern);
index = KMP(str, pattern, next);
printf("%d\n", index);
}
return ;
} /*
测试用例
3
abcabcbababcaba
ababcaba
abcabcbababcaba
ababcaba
abcabcbababcaba
ababcaba */

2017年12月11日08:54:05

今天再来看字符串匹配的时候,KMP又忘记了,现在知道的是:

  对于短字符串来说,匹配的时候,会计算它的部分匹配表(即,前缀和后缀相同时,串的最大长度)

  当匹配时,需要用 已经移动了的长度 -  部分匹配长度

详细请看

http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html

class Solution(object):
def BF_Match(self, haystack, needle):
hlen = len(haystack)
nlen = len(needle) if hlen >= nlen:
for k in xrange(hlen - nlen + 1):
i = k
j = 0
while i < hlen and j < nlen and haystack[i] == needle[j]:
i += 1
j += 1
if j == nlen:
return k
else:
continue
return -1 def pmt(self, needle):
"""
PartialMatchTable
:param needle:
"""
nlen = len(needle)
prefix = [needle[:i+1] for i in xrange(len(needle)-1)]
print prefix
postfix = [needle[i+1:] for i in xrange(len(needle)-1)]
print postfix intersection = list(set(prefix) & set(postfix))
maxlen = 0
for i in xrange(0,len(intersection)):
if maxlen < len(intersection[i]):
maxlen = len(intersection[i])
if intersection:
#print intersection,maxlen
return maxlen
return 0 def kmp(self, haystack, needle):
i = 0
hlen = len(haystack)
nlen = len(needle)
while i < hlen-nlen+1:
match = True
for j in xrange(nlen):
if haystack[i+j] != needle[j]:
match = False
break
if match:
return i
if j:
i += j - self.pmt(needle[:j])
else:
i += 1
return -1 def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(haystack) >= 0 and len(needle) == 0:
return 0 if len(haystack) < len(needle):
return -1 for i in xrange(0, len(haystack)):
tmpi = i
cnt = 0
for j in xrange(0, len(needle)):
if(tmpi < len(haystack)) and haystack[tmpi] == needle[j]:
tmpi += 1
j += 1
cnt += 1
if len(needle) == cnt:
return i return -1 haystack = "aabaaabaaac" #haystack = "ababcaababcaabc" needle ="aabaaac" s = Solution()
print s.kmp(haystack, needle)

  

POJ3461 KMP 模板题的更多相关文章

  1. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  2. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  3. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  4. (模板)poj3461(kmp模板题)

    题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...

  5. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  6. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  7. HDU 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  8. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  9. poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

随机推荐

  1. 关于JQ toggle 的注意事项

    1.9.1以后的版本,好像不支持 jq 的 toggle function的用法啦.

  2. Dom元素的操作

    getElementById(): 获取有指定惟一ID属性值文档中的元素 getElementsByName(name): 返回的是数组 getElementsByTagName(): 返回具有指定标 ...

  3. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  4. find命令

    http://www.jb51.net/os/RedHat/1307.html find 目录(.代表当前目录) -type d -name "..."    f -name &q ...

  5. Visual Studio 常用快捷键备忘

    在代码中插入书签 用途 操作   vs2013 快速在自定义的不同代码位置跳转 首先点击: 编辑=>书签=>启用书签 然后再在代码编辑窗口 ctrl+k, k (取消书签,再按一次 ctr ...

  6. 常用ADO.NET操作ACCESS数据库

    using System; using System.Collections.Generic; using System.Text; using System.Data;// using System ...

  7. MVC中使用Entity Framework 基于方法的查询学习笔记 (三)

    紧接上文,我们已经学习了MVC数据上下文中两个常用的类,这两个类承载着利用函数方式进行数据查询的全部内容,我们既然已经了解了DbSet<TEntity> 是一个泛型集合,并且实现了一些接口 ...

  8. POJ2777

    http://poj.org/problem?id=2777 前几天看到一个大神说,要搞,就搞专题或者套题,我觉得现在这么菜的我,还是搞搞专题吧. 一道比较裸也比较基础的线段树的题目. 题意:就是有一 ...

  9. SQL表值函数(上月添加1-28)

    ALTER function [dbo].[fn_getdate3] ( ) ) RETURNS @Table_Var TABLE ( LastTime datetime ) as begin Dec ...

  10. Java注解基础概念总结

    注解的概念 注解(Annotation),也叫元数据(Metadata),是Java5的新特性,JDK5引入了Metadata很容易的就能够调用Annotations.注解与类.接口.枚举在同一个层次 ...