摘要: 题意:给出两个数n,nc,并给出一个由nc种字符组成的字符串。求这个字符串中长度为n的子串有多少种。分析:1.这个题不用匹配,因为不高效。2.将长度为n的子串看作n位的nc进制数,将问题转化为共有多少种十进制数字。3.哈希时,每一个字符都对应这0---nc-1的一个数字。代码:View Code 1 #include <iostream> 2 #include <stdio.h> 3 #include <memory.h> 4 using namespace std; 5 const int maxnum=1600000; 6 char str[maxnum 阅读全文
posted @ 2012-08-15 12:38 pushing my way 阅读(1009) 评论(0) 推荐(0) 编辑
摘要: 字符串匹配算法有四种:1.朴素算法,预处理O(0),匹配时间O((n-m+1)m) 其中n是文本长度,m是模式长度2.Rabin-Karp算法,预处理O(m),匹配时间同朴素算法3.有限自动机算法,预处理O(m|∑|),匹配时间O(n)4.KMP算法,预处理O(m),匹配时间O(n)这里讨论的是有限自动机算法,先给出预处理为O(m^3|∑|),匹配时间为O(n)的算法。代码:View Code 1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 using namespace st 阅读全文
posted @ 2012-08-15 11:00 pushing my way 阅读(664) 评论(0) 推荐(0) 编辑