摘要: KMP算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,其对于任何模式和目标序列,都可以在线性时间内完成匹配查找,而不会发生退化,是一个非常优秀的模式匹配算法。但是相较于其他模式匹配算法,该算法晦涩难懂,第一次接触该算法的读者往往会看得一头雾水,主要原因是KMP算法在构造跳转表next过程中进行了多个层面的优化和抽象,使得KMP算法进行模式匹配的原理显得不那么直白。本文希望能够深入KMP算法,将该算法的各个细节彻底讲透,扫除读者对该算法的困扰。KMP算法对于朴素匹配算法的改进是引入了一个跳转表next[]。以模式字符串abcabcacab为例,其跳转表为:j12345678 阅读全文
posted @ 2013-03-29 19:39 life91 阅读(166) 评论(0) 推荐(0) 编辑
摘要: 获得不大于数的2整数幂的数。例如, 不大于6的2整数幂的数是4. 1 #include <bitset> 2 3 using namespace std; 4 5 /* 6 * 返回不大于num的最大数的2进制数幂次。 7 */ 8 int GetMaxPos(int num) 9 {10 int flag = num & (num - 1);11 12 if (flag == 0)13 flag = num;14 15 int pos = 0;16 while (flag >>= 1) {17 pos++;18 ... 阅读全文
posted @ 2013-03-23 10:51 life91 阅读(231) 评论(0) 推荐(0) 编辑
摘要: 问题:将字符串取反。例如,“abcd” ---> "dcba"这里的思想取自合并排序。下面的源码存在一个问题。目前我还没有解决掉,欢迎大家指导!! 1 /* 2 * Q: 将字符串取反。例如, 3 * “abcd” --> "dcba" 4 */ 5 #include <iostream> 6 #include <cstring> 7 8 using namespace std; 9 10 void Merge(char *str, int start, int mid, int end)11 {12 int lenl 阅读全文
posted @ 2013-03-22 16:07 life91 阅读(1133) 评论(0) 推荐(0) 编辑
摘要: 1 #include <stdio.h> 2 3 // 除法 4 int div(int loperand, int roperand) 5 { 6 int cnt = 0; 7 while (loperand > roperand) 8 { 9 cnt++;10 loperand -= roperand;11 }12 13 return cnt;14 }15 16 // 取余数17 int getRemainder(int loperand, int roperand)18 {19 while (lopera... 阅读全文
posted @ 2013-03-21 18:38 life91 阅读(217) 评论(0) 推荐(0) 编辑
摘要: 参考书籍《Python核心编程》 下划线(_)在解释器重表示最后一个表示式的值。 可以使用C语言风格的格式符号来格式化输入数据。raw_input()读取标准输入,并将读取到的数据赋值给指定的变量。例如, user = raw_input(‘Enter login name: ‘) print表示输出数据。例如,print ‘This is name: %s’ %user 用#作为行注释,从#开始一直到一行结束的内容都是注释。还有一种叫做文档字符串的特别注释,在模块、类或者函数的起始添加一个字符串,起到在线文档的功能。 Python的标准算术运算符: + - * / // % ** 其中... 阅读全文
posted @ 2013-03-18 20:15 life91 阅读(460) 评论(0) 推荐(0) 编辑