字符串匹配算法SMA 总结之四:自动机算法

自动机匹配算法常使用链表表示,这里使用数组,占用的空间太大了些,

有时间详细说说

#include <iostream> using namespace std; const int alphabet_len=128; //construct an automation /*int*/void preAuto(int* automation,const char* pattern,const int pattern_len) { //int state=0; for (int i=0;i<pattern_len;i++){ int reserve=automation[i*alphabet_len+pattern[i]]; automation[i*alphabet_len+pattern[i]]=i+1; memcpy(automation+(i+1)*alphabet_len,automation+reserve*alphabet_len,alphabet_len*sizeof(int)); } } int dfa(const char* text,const int text_len,const char* pattern,const int pattern_len) { //allocate memory for automation int* automation=new int[(pattern_len+1)*alphabet_len]; //initialization for (int i=0;i<(pattern_len+1)*alphabet_len;i++){ automation[i]=0; } //preprocessing preAuto(automation,pattern,pattern_len); int result=-1; //searching int auto_state=0; for (int i=0;i<text_len;i++){ auto_state=automation[auto_state*alphabet_len+text[i]]; if (auto_state==pattern_len){ result= i-pattern_len+1; break; } } delete[] automation; return result; }

 

你可能感兴趣的:(字符串匹配算法SMA 总结之四:自动机算法)