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

数据结构昨天重新学习了一下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. Python购物车程序

    1.要求用户输入工资,然后打印购物菜单 2.用户可以不断的购买商品,直到钱不够为止 3.退出时格式化打印用户已购买的商品和剩余金额 salary = int(input("请输入你的工资:& ...

  2. jsp 头像上传显示部分代码实现

    <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> <% ...

  3. 修改Centos 6.5的yum源

    1.进入目录 cd /etc/yum.repos.d/ 2.保持副本 mv CentOS-Base.repo CentOS-Base.repo.backup 3.下载新的CentOS-Base.rep ...

  4. Android Studio Error:CreateProcess error=216

    Error:CreateProcess error=216, This version of %1 is not compatible with the version of Windows you' ...

  5. Linux下的压缩和解压缩命令——gzip/gunzip

    gzip命令 gzip命令用来压缩文件.gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处".gz"扩展名. gzip是在Linux系统中经常使用的一个对文件进行压 ...

  6. Android手机刷recovery

    以前觉得android刷机是件很麻烦的事,现在倒不觉得了.  只要手机刷入第三方的recovery,一切都好办了,无论是root还是刷google play.  recovery开源的有两大阵营,tw ...

  7. powershell通过wps excel导出csv

    powershell比较强大,比较好用,比较方便. $et=New-Object -ComObject et.application #$et.Visible=$true $et.DisplayAle ...

  8. c#延迟加载

    public class BlogUser { public int Id { get; private set; } public Lazy<List<Article>> A ...

  9. win10 安装framework3.5

    win+x 点击命令提示符(个管理员) 输入dism.exe /online /enable-feature /featurename:NetFX3 /Source:H:\sources\sxs(解压 ...

  10. c++拷贝构造和编译优化

    #include <iostream> using namespace std; class MyClass { public: MyClass(); MyClass(int i); My ...