資料結構開發(14):KMP 子串查詢演算法
阿新 • • 發佈:2018-12-19
0.目錄
1.KMP 子串查詢演算法
2.KMP 演算法的應用
3.小結
1.KMP 子串查詢演算法
問題:
如何在目標字串S中,查詢是否存在子串P?
樸素解法:
樸素解法的一個優化線索:
示例:
偉大的發現:
- 匹配失敗時的右移位數與子串本身相關,與目標串無關
- 移動位數 = 已匹配的字元數 - 對應的部分匹配值
- 任意子串都存在一個唯一的部分匹配表
部分匹配表示例:
問題:
部分匹配表是怎麼得到的?
- 字首
- 除了最後一個字元以外,一個字串的全部頭部組合
- 字尾
- 除了第一個字元以外,一個字串的全部尾部組合
- 部分匹配值
- 字首和字尾最長共有元素的長度
示例:ABCDABD
問題:
- 怎麼程式設計產生部分匹配表?
實現關鍵:
- PMT[1] = 0 ( 下標為0的元素匹配值為0 )
- 從 2 個字元開始遞推 ( 從下標為 1 的字元開始遞推 )
- 假設 PMT[n] = PMT[n-1] + 1 ( 最長共有元素的長度 )
- 當假設不成立,PMT[n] 在 PMT[n-1] 的基礎上減小
程式設計產生部分匹配表:
(ll代表longest length,即最長共有元素的長度。推導過程遵循下列原則:
(1). 當前欲求的ll值,通過歷史ll值推導。
(2). 當可選ll值為0時,直接比對首尾元素。
在求ababax的最後一項ll值時,
字首為aba b,
字尾為aba x。
重疊部分的長度就是當前的ll值,即:3;PMT(3)的含義是查詢3個字元時的ll值,而3個字元時的ll值對應著下標為2的情形;程式設計實現時注意長度與下標的對應關係。)
#include <iostream> #include <cstring> using namespace std; int* make_pmt(const char* p) { int len = strlen(p); int* ret = static_cast<int*>(malloc(sizeof(int) * len)); if( ret != NULL ) { int ll = 0; ret[0] = 0; for(int i=1; i<len; i++) { while( (ll > 0) && (p[ll] != p[i]) ) { ll = ret[ll-1]; } if( p[ll] == p[i] ) { ll++; } ret[i] = ll; } } return ret; } int main() { int* pmt_1 = make_pmt("ababax"); cout << "ababax:" << endl; for(int i=0; i<strlen("ababax"); i++) { cout << i << " : " << pmt_1[i] << endl; } cout << endl; int* pmt_2 = make_pmt("ABCDABD"); cout << "ABCDABD:" << endl; for(int i=0; i<strlen("ABCDABD"); i++) { cout << i << " : " << pmt_2[i] << endl; } return 0; }
執行結果為:
ababax:
0 : 0
1 : 0
2 : 1
3 : 2
4 : 3
5 : 0
ABCDABD:
0 : 0
1 : 0
2 : 0
3 : 0
4 : 1
5 : 2
6 : 0
部分匹配表的使用 ( KMP 演算法 ):
實現KMP演算法:
#include <iostream>
#include <cstring>
using namespace std;
int* make_pmt(const char* p)
{
int len = strlen(p);
int* ret = static_cast<int*>(malloc(sizeof(int) * len));
if( (ret != NULL) && (len > 0) )
{
int ll = 0;
ret[0] = 0;
for(int i=1; i<len; i++)
{
while( (ll > 0) && (p[ll] != p[i]) )
{
ll = ret[ll-1];
}
if( p[ll] == p[i] )
{
ll++;
}
ret[i] = ll;
}
}
return ret;
}
int kmp(const char* s, const char* p)
{
int ret = -1;
int sl = strlen(s);
int pl = strlen(p);
int* pmt = make_pmt(p);
if( (pmt != NULL) && (0 < pl) && (pl <= sl) )
{
for(int i=0, j=0; i<sl; i++)
{
while( (j > 0) && (s[i] != p[j]) )
{
j = pmt[j-1];
}
if( s[i] == p[j] )
{
j++;
}
if( j == pl )
{
ret = i + 1 - pl;
break;
}
}
}
free(pmt);
return ret;
}
int main()
{
cout << kmp("abcde", "cde") << endl;
cout << kmp("ababax", "ba") << endl;
cout << kmp("ababax", "ax") << endl;
cout << kmp("ababax", "") << endl;
cout << kmp("ababax", "ababaxy") << endl;
return 0;
}
執行結果為:
2
1
4
-1
-1
2.KMP 演算法的應用
思考:
- 如何在目標字串中查詢是否存在指定的子串?
字串類中的新功能:
將kmp演算法的程式碼整合到自定義字串類中去:
protected:
static int* make_pmt(const char* p);
static int kmp(const char* s, const char* p);
具體實現:
int* String::make_pmt(const char* p)
{
int len = strlen(p);
int* ret = static_cast<int*>(malloc(sizeof(int) * len));
if( (ret != NULL) && (len > 0) )
{
int ll = 0;
ret[0] = 0;
for(int i=1; i<len; i++)
{
while( (ll > 0) && (p[ll] != p[i]) )
{
ll = ret[ll-1];
}
if( p[ll] == p[i] )
{
ll++;
}
ret[i] = ll;
}
}
return ret;
}
int String::kmp(const char* s, const char* p)
{
int ret = -1;
int sl = strlen(s);
int pl = strlen(p);
int* pmt = make_pmt(p);
if( (pmt != NULL) && (0 < pl) && (pl <= sl) )
{
for(int i=0, j=0; i<sl; i++)
{
while( (j > 0) && (s[i] != p[j]) )
{
j = pmt[j-1];
}
if( s[i] == p[j] )
{
j++;
}
if( j == pl )
{
ret = i + 1 - pl;
break;
}
}
}
free(pmt);
return ret;
}
子串查詢 ( KMP 演算法的直接運用 ):
- int indexOf(const char* s) const
- int indexOf(const String& s) const
子串查詢:
public:
int indexOf(const char* s) const;
int indexOf(const String& s) const;
具體實現:
int String::indexOf(const char* s) const
{
return kmp(m_str, s ? s : "");
}
int String::indexOf(const String& s) const
{
return kmp(m_str, s.m_str);
}
在字串中將指定的子串刪除:
- String& remove(const char* s)
- String& remove(const String& s)
在字串中將指定的子串刪除:
public:
String& remove(int i, int len);
String& remove(const char* s);
String& remove(const String& s);
具體實現:
String& String::remove(int i, int len)
{
if( (0 <= i) && (i < m_length) )
{
int n = i;
int m = i + len;
while( (n < m) && (m < m_length) )
{
m_str[n++] = m_str[m++];
}
m_str[n] = '\0';
m_length = n;
}
return *this;
}
String& String::remove(const char* s)
{
return remove(indexOf(s), s ? strlen(s) : 0);
}
String& String::remove(const String& s)
{
return remove(indexOf(s), s.length());
}
字串的減法操作定義 ( operator - ):
- 使用 remove 實現字串間的減法操作
- 字串自身不被修改
- 返回產生的新串
字串的減法操作定義:
public:
String operator - (const String& s) const;
String operator - (const char* s) const;
String& operator -= (const String& s);
String& operator -= (const char* s);
具體實現:
String String::operator - (const String& s) const
{
return String(*this).remove(s);
}
String String::operator - (const char* s) const
{
return String(*this).remove(s);
}
String& String::operator -= (const String& s)
{
return remove(s);
}
String& String::operator -= (const char* s)
{
return remove(s);
}
字串中的子串替換:
- String& replace(const char* t, const char* s)
- String& replace(const String& t, const char* s)
- String& replace(const char* t, const String& s)
- String& replace(const String& t, const String& s)
字串中的子串替換:
public:
String& replace(const char* t, const char* s);
String& replace(const String& t, const char* s);
String& replace(const char* t, const String& s);
String& replace(const String& t, const String& s);
具體實現:
String& String::replace(const char* t, const char* s)
{
int index = indexOf(t);
if( index >= 0 )
{
remove(t);
insert(index, s);
}
return *this;
}
String& String::replace(const String& t, const char* s)
{
return replace(t.m_str, s);
}
String& String::replace(const char* t, const String& s)
{
return replace(t, s.m_str);
}
String& String::replace(const String& t, const String& s)
{
return replace(t.m_str, s.m_str);
}
從字串中建立子串:
- String sub(int i, int len) const
- 以 i 為起點提取長度為 len 的子串
- 子串提取不會改變字串本身的狀態
從字串中建立子串:
public:
String sub(int i, int len) const;
具體實現:
String String::sub(int i, int len) const
{
String ret;
if( (0 <= i) && (i < m_length) )
{
if( len < 0 ) len = 0;
if( len + i > m_length ) len = m_length - i;
char* str = reinterpret_cast<char*>(malloc(len + 1));
strncpy(str, m_str + i, len);
str[len] = '\0';
ret = str;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
}
return ret;
}
3.小結
- 部分匹配表是提高子串查詢效率的關鍵
- 部分匹配值定義為字首和字尾最長共有元素的長度
- 可以用遞推的方法產生部分匹配表
- KMP 利用部分匹配值與子串移動位數的關係提高查詢效率
- 字串類是工程開發中必不可少的元件
- 字串中應該包含常用字串操作函式
- 增 : insert , operator + , ...
- 刪 : remove , operator - , ...
- 查 : indexOf , ...
- 改 : replace , ...
最終的自定義字串類程式碼:
StString.h
#ifndef STSTRING_H
#define STSTRING_H
#include "Object.h"
namespace StLib
{
class String : public Object
{
protected:
char* m_str;
int m_length;
void init(const char* s);
bool equal(const char* l, const char* r, int len) const;
static int* make_pmt(const char* p);
static int kmp(const char* s, const char* p);
public:
String();
String(char c);
String(const char* s);
String(const String& s);
int length() const;
const char* str() const;
bool startWith(const char* s) const;
bool startWith(const String& s) const;
bool endOf(const char* s) const;
bool endOf(const String& s) const;
String& insert(int i, const char* s);
String& insert(int i, const String& s);
String& trim();
int indexOf(const char* s) const;
int indexOf(const String& s) const;
String& remove(int i, int len);
String& remove(const char* s);
String& remove(const String& s);
String& replace(const char* t, const char* s);
String& replace(const String& t, const char* s);
String& replace(const char* t, const String& s);
String& replace(const String& t, const String& s);
String sub(int i, int len) const;
char& operator [] (int i);
char operator [] (int i) const;
bool operator == (const String& s) const;
bool operator == (const char* s) const;
bool operator != (const String& s) const;
bool operator != (const char* s) const;
bool operator > (const String& s) const;
bool operator > (const char* s) const;
bool operator < (const String& s) const;
bool operator < (const char* s) const;
bool operator >= (const String& s) const;
bool operator >= (const char* s) const;
bool operator <= (const String& s) const;
bool operator <= (const char* s) const;
String operator + (const String& s) const;
String operator + (const char* s) const;
String& operator += (const String& s);
String& operator += (const char* s);
String operator - (const String& s) const;
String operator - (const char* s) const;
String& operator -= (const String& s);
String& operator -= (const char* s);
String& operator = (const String& s);
String& operator = (const char* s);
String& operator = (char c);
~String();
};
}
#endif // STSTRING_H
StString.cpp
#include <cstring>
#include <cstdlib>
#include "StString.h"
#include "Exception.h"
using namespace std;
namespace StLib
{
int* String::make_pmt(const char* p)
{
int len = strlen(p);
int* ret = static_cast<int*>(malloc(sizeof(int) * len));
if( (ret != NULL) && (len > 0) )
{
int ll = 0;
ret[0] = 0;
for(int i=1; i<len; i++)
{
while( (ll > 0) && (p[ll] != p[i]) )
{
ll = ret[ll-1];
}
if( p[ll] == p[i] )
{
ll++;
}
ret[i] = ll;
}
}
return ret;
}
int String::kmp(const char* s, const char* p)
{
int ret = -1;
int sl = strlen(s);
int pl = strlen(p);
int* pmt = make_pmt(p);
if( (pmt != NULL) && (0 < pl) && (pl <= sl) )
{
for(int i=0, j=0; i<sl; i++)
{
while( (j > 0) && (s[i] != p[j]) )
{
j = pmt[j-1];
}
if( s[i] == p[j] )
{
j++;
}
if( j == pl )
{
ret = i + 1 - pl;
break;
}
}
}
free(pmt);
return ret;
}
void String::init(const char *s)
{
m_str = strdup(s);
if( m_str )
{
m_length = strlen(m_str);
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create String object ...");
}
}
String::String()
{
init("");
}
String::String(char c)
{
char s[] = {c, '\0'};
init(s);
}
String::String(const char *s)
{
init(s ? s : "");
}
String::String(const String &s)
{
init(s.m_str);
}
int String::length() const
{
return m_length;
}
const char* String::str() const
{
return m_str;
}
bool String::equal(const char* l, const char* r, int len) const
{
bool ret = true;
for(int i=0; i<len && ret; i++)
{
ret = ret && (l[i] == r[i]);
}
return ret;
}
bool String::startWith(const char* s) const
{
bool ret = (s != NULL);
if( ret )
{
int len = strlen(s);
ret = (len < m_length) && equal(m_str, s, len);
}
return ret;
}
bool String::startWith(const String& s) const
{
return startWith(s.m_str);
}
bool String::endOf(const char* s) const
{
bool ret = (s != NULL);
if( ret )
{
int len = strlen(s);
char* str = m_str + (m_length - len);
ret = (len < m_length) && equal(str, s, len);
}
return ret;
}
bool String::endOf(const String& s) const
{
return endOf(s.m_str);
}
String& String::insert(int i, const char* s)
{
if( (0 <= i) && (i <= m_length) )
{
if( (s != NULL) && (s[0] != '\0') )
{
int len = strlen(s);
char* str = reinterpret_cast<char*>(malloc(m_length + len + 1));
if( str != NULL )
{
strncpy(str, m_str, i);
strncpy(str + i, s, len);
strncpy(str + i + len, m_str + i, m_length - i);
str[m_length + len] = '\0';
free(m_str);
m_str = str;
m_length = m_length + len;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to insert string value ...");
}
}
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
}
return *this;
}
String& String::insert(int i, const String& s)
{
return insert(i, s.m_str);
}
String& String::trim()
{
int b = 0;
int e = m_length - 1;
while( m_str[b] == ' ' ) b++;
while( m_str[e] == ' ' ) e--;
if( b == 0 )
{
m_str[e + 1] = '\0';
m_length = e + 1;
}
else
{
for(int i=0, j=b; j<=e; i++, j++)
{
m_str[i] = m_str[j];
}
m_str[e - b + 1] = '\0';
m_length = e - b + 1;
}
return *this;
}
int String::indexOf(const char* s) const
{
return kmp(m_str, s ? s : "");
}
int String::indexOf(const String& s) const
{
return kmp(m_str, s.m_str);
}
String& String::remove(int i, int len)
{
if( (0 <= i) && (i < m_length) )
{
int n = i;
int m = i + len;
while( (n < m) && (m < m_length) )
{
m_str[n++] = m_str[m++];
}
m_str[n] = '\0';
m_length = n;
}
return *this;
}
String& String::remove(const char* s)
{
return remove(indexOf(s), s ? strlen(s) : 0);
}
String& String::remove(const String& s)
{
return remove(indexOf(s), s.length());
}
String& String::replace(const char* t, const char* s)
{
int index = indexOf(t);
if( index >= 0 )
{
remove(t);
insert(index, s);
}
return *this;
}
String& String::replace(const String& t, const char* s)
{
return replace(t.m_str, s);
}
String& String::replace(const char* t, const String& s)
{
return replace(t, s.m_str);
}
String& String::replace(const String& t, const String& s)
{
return replace(t.m_str, s.m_str);
}
String String::sub(int i, int len) const
{
String ret;
if( (0 <= i) && (i < m_length) )
{
if( len < 0 ) len = 0;
if( len + i > m_length ) len = m_length - i;
char* str = reinterpret_cast<char*>(malloc(len + 1));
strncpy(str, m_str + i, len);
str[len] = '\0';
ret = str;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
}
return ret;
}
char& String::operator [] (int i)
{
if( (0 <= i) && (i < m_length) )
{
return m_str[i];
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
}
}
char String::operator [] (int i) const
{
return (const_cast<String&>(*this))[i];
}
bool String::operator == (const String& s) const
{
return (strcmp(m_str, s.m_str) == 0);
}
bool String::operator == (const char* s) const
{
return (strcmp(m_str, s ? s : "") == 0);
}
bool String::operator != (const String& s) const
{
return !(*this == s);
}
bool String::operator != (const char* s) const
{
return !(*this == s);
}
bool String::operator > (const String& s) const
{
return (strcmp(m_str, s.m_str) > 0);
}
bool String::operator > (const char* s) const
{
return (strcmp(m_str, s ? s : "") > 0);
}
bool String::operator < (const String& s) const
{
return (strcmp(m_str, s.m_str) < 0);
}
bool String::operator < (const char* s) const
{
return (strcmp(m_str, s ? s : "") < 0);
}
bool String::operator >= (const String& s) const
{
return (strcmp(m_str, s.m_str) >= 0);
}
bool String::operator >= (const char* s) const
{
return (strcmp(m_str, s ? s : "") >= 0);
}
bool String::operator <= (const String& s) const
{
return (strcmp(m_str, s.m_str) <= 0);
}
bool String::operator <= (const char* s) const
{
return (strcmp(m_str, s ? s : "") <= 0);
}
String String::operator + (const String& s) const
{
return (*this + s.m_str);
}
String String::operator + (const char* s) const
{
String ret;
int len = m_length + strlen(s ? s : "");
char* str = reinterpret_cast<char*>(malloc(len + 1));
if( str )
{
strcpy(str, m_str);
strcat(str, s ? s : "");
free(ret.m_str);
ret.m_str = str;
ret.m_length = len;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to add String values ...");
}
return ret;
}
String& String::operator += (const String& s)
{
return (*this = *this + s.m_str);
}
String& String::operator += (const char* s)
{
return (*this = *this + s);
}
String String::operator - (const String& s) const
{
return String(*this).remove(s);
}
String String::operator - (const char* s) const
{
return String(*this).remove(s);
}
String& String::operator -= (const String& s)
{
return remove(s);
}
String& String::operator -= (const char* s)
{
return remove(s);
}
String& String::operator = (const String& s)
{
return (*this = s.m_str);
}
String& String::operator = (const char* s)
{
if( m_str != s )
{
char* str = strdup(s ? s : "");
if( str )
{
free(m_str);
m_str = str;
m_length = strlen(m_str);
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to assign new String value ...");
}
}
return *this;
}
String& String::operator = (char c)
{
char s[] = {c, '\0'};
return (*this = s);
}
String::~String()
{
free(m_str);
}
}