KMP

char a b a b a b c a
index 0 1 2 3 4 5 6 7
value 0 0 1 2 3 4 0 1

"slash"

proper profix: s, sl, sla, slas

proper suffix: h, sh, ash, lash

partial match table(也有人叫失配函数,也有人叫next数组):

每个index表示字符串是一个子串,子串的下标∈(0, index)

value = "proper profix"和"proper suffix"相同的最长字符串

举例:
index = 3处, proper profix: a, ab, aba;  proper suffix:b, ab, bab;

value = "ab"的长度 = 2

实现:

 1 void computeLPSArray(char *pat, int M, int *lps)
 2 {
 3     // length of the previous longest prefix suffix
 4     int len = 0;
 5
 6     lps[0] = 0; // lps[0] is always 0
 7
 8     // the loop calculates lps[i] for i = 1 to M-1
 9     int i = 1;
10     while (i < M)
11     {
12         if (pat[i] == pat[len])
13         {
14             len++;
15             lps[i] = len;
16             i++;
17         }
18         else // (pat[i] != pat[len])
19         {
20             // This is tricky. Consider the example.
21             // AAACAAAA and i = 7. The idea is similar
22             // to search step.
23             if (len != 0)
24             {
25                 len = lps[len-1];
26
27                 // Also, note that we do not increment
28                 // i here
29             }
30             else // if (len == 0)
31             {
32                 lps[i] = 0;
33                 i++;
34             }
35         }
36     }
37 }

模式字符串移动:

当table[partial_match_length] > 1, 移动partial_match_length - table[partial_match_length - 1]

KMP search实现:

 1 void KMPSearch(char *pat, char *txt)
 2 {
 3     int M = strlen(pat);
 4     int N = strlen(txt);
 5
 6     // create lps[] that will hold the longest prefix suffix
 7     // values for pattern
 8     int lps[M];
 9
10     // Preprocess the pattern (calculate lps[] array)
11     computeLPSArray(pat, M, lps);
12
13     int i = 0;  // index for txt[]
14     int j  = 0;  // index for pat[]
15     while (i < N)
16     {
17         if (pat[j] == txt[i])
18         {
19             j++;
20             i++;
21         }
22
23         if (j == M)
24         {
25             printf("Found pattern at index %d \n", i-j);
26             j = lps[j-1];
27         }
28
29         // mismatch after j matches
30         else if (i < N && pat[j] != txt[i])
31         {
32             // Do not match lps[0..lps[j-1]] characters,
33             // they will match anyway
34             if (j != 0)
35                 j = lps[j-1];
36             else
37                 i = i+1;
38         }
39     }
40 }

紧凑的实现:

 1 void preparation(char *P, int *f) {
 2     int m = strlen(P);
 3     f[0] = f[1] = 0;
 4     for (int i = 1; i < m; i++) {
 5         int j = f[i];
 6         while (j && P[i] != P[j])
 7               j = f[j];
 8         f[i + 1] = (P[i] == P[j])? j+1 : 0;
 9     }
10 }
11 void KMP(char *T, char *P, int *f) {
12     int n = strlen(T), m = strlen(P);
13     preparation(P, f);
14     int j = 0;
15     for (int i = 0; i < n; i++) {
16         while (j && P[i] != T[i]) j = f[j];
17         if (P[j] == T[i]) j++;
18         if (j == m) answer(i - m + 1);
19     }
20 }

参考:

http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/

http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm

AC 自动机

背景:基于有限状态自动机

KMP的partial match table那么多叫法什么失败指针,失配函数,就来源于AC自动机。

参考:http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

http://www.cnblogs.com/en-heng/p/5247903.html

转载于:https://www.cnblogs.com/autoria/p/6013366.html

String Algorithm相关推荐

  1. 分隔和截断字符串, boost string algorithm library中的split和trim

    http://www.boost.org/doc/libs/1_46_1/doc/html/string_algo.html 这个库是个 headers only library 这个库提供了STL没 ...

  2. B1019/A1069 数字黑洞 借用string, algorithm快速完成

    通过大量库函数快速完成此题. 比如 sort对字符串排序 string.length()来对不足4位的数字补0 to_string()将string快速转为int (还有个astox函数可以实现str ...

  3. 使用RSA私钥或pfx私钥签名String

    项目有个需求,使用私钥签名请求body内容,放在请求头部,作为头部一个字段内容请求外部服务,签名有二种方式,对方提供私钥串/直接提供pfx私钥文件. 一. 提供私钥串  示例代码如下: public ...

  4. 【SearchString Algorithm Training】谭爷剪花布条

    [Search&String Algorithm Training]- I "对不起,你是个好人..." 手上的玫瑰花缓缓掉落,谭爷看着妹子远去的背影,呆呆地站着. &qu ...

  5. c++,string,compare,nocase,for copy

    总结:如何在c++中进行大小写忽略的比较,基于std:string ref1,ref2,ref3 问题由来: 标准字符 (typedef basic_string<char> string ...

  6. php pfx rsa pem,使用RSA私钥或pfx私钥签名String

    /** * @param algorithm 签名算法: SHA1WithRSA / MD5withRSA等 * @param password 密码 * @param privateKeyPath ...

  7. java签名算法阻止 设置_java数字签名算法之RSA

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml xsi:schemaLocation="http://maven.apache.org/POM/4. ...

  8. 3des java 库_java 3DES 加密

    public class DESCode { private String algorithm = "DESede/CBC/PKCS7Padding";//加密方法/运算模式/填充 ...

  9. 密码学研究-数字签名

    引入: 提到签名,大家都不陌生,大家知道,重大的文件一般都要领导签名,来确保这个文件的真实有效.而一些比较重要的合同,比如买房的购房合同,都要盖"骑缝章",这个骑缝章,就是盖在2页 ...

最新文章

  1. 真没想到中国有这么猛的软件,杀伤力太强了!
  2. 7.13 T2 Shit 题(shit)
  3. 我的年龄又快被5整除了......
  4. el表达式的语法_「手把手教python3接口自动化」「第三章」:Python3 语法
  5. Angular里如何测试一个具有外部依赖的Component
  6. Mysql 5.5的编译安装 在ubuntu 10平台上面
  7. 删除office2016专业版多余组件
  8. 用python写个电子钟_[TPYBoard - Micropython之会python就能做硬件 3] 制作电子时钟
  9. python 监听tcp端口_创建TCP监听_创建TCP监听_功能示例_Python SDK示例_SDK 参考_开发指南_负载均衡 - 阿里云...
  10. 飘刃 0.1.1 发布,速度碾压 Vue-CLI 的轻量级 Vue 项目构建工具
  11. 字符串的数组形式与指针形式
  12. matlab创建数组
  13. 异常0x0000005
  14. 如何优化ASO让app在苹果上获得更多量
  15. 多目标、多阶段、多层次的强化学习合作方法
  16. Java+Springboot+Mybatis+Mysql+Bootstrap+Maven实现景区旅游管理系统
  17. mysql数据库所有表合并_mysql数据库如何将表合并我从数据库中倒出表导出后是这样的b 爱问知识人...
  18. word文档在保存后消失,如何恢复?
  19. Git及Github之入门到进阶
  20. 大脑神经网络具有什么性,神经网络跟大脑的关系

热门文章

  1. 2018年度计算机视觉GtiHub top开源项目!
  2. 关于文件整理的一些心得
  3. nfc 过滤 android,android-NFC意图过滤器= I / NfcDispatcher(923):连...
  4. 土方工程量计算表格excel_工程造价算量表+工程量软件,超多表格可套用,高清下载...
  5. chap01 .net 基本框架介绍
  6. 项目Beta冲刺Day3
  7. java获取请求的url地址
  8. Memcache 安装
  9. Java线程之CompletionService批处理任务
  10. U-LINK2 升级后低版本不识别问题