ในบทความนี้ เราจะพูดถึงโปรแกรมที่ใช้อัลกอริทึม Finite Automata สำหรับการค้นหารูปแบบ
เรามีข้อความ[0...n-1] และรูปแบบ[0...m-1] เราต้องหารูปแบบทั้งหมดที่เกิดขึ้น[]ในข้อความ[].
สำหรับสิ่งนี้ เราจะประมวลผลข้อความล่วงหน้า[] และสร้างอาร์เรย์ 2 มิติเพื่อนำเสนอ หลังจากนั้น เราก็แค่ต้องข้ามผ่านระหว่างองค์ประกอบของข้อความ[] และสถานะต่างๆ ของออโตมาตะ
ตัวอย่าง
#include<stdio.h>
#include<string.h>
#define total_chars 256
int calc_nextstate(char *pat, int M, int state, int x) {
if (state < M && x == pat[state])
return state+1;
int ns, i;
for (ns = state; ns > 0; ns--) {
if (pat[ns-1] == x) {
for (i = 0; i < ns-1; i++)
if (pat[i] != pat[state-ns+1+i])
break;
if (i == ns-1)
return ns;
}
}
return 0;
}
//builds Finite Automata
void calc_TF(char *pat, int M, int TF[][total_chars]) {
int state, x;
for (state = 0; state <= M; ++state)
for (x = 0; x < total_chars; ++x)
TF[state][x] = calc_nextstate(pat, M, state, x);
}
//prints all occurrences of pattern in text
void calc_occur(char *pat, char *txt) {
int M = strlen(pat);
int N = strlen(txt);
int TF[M+1][total_chars];
calc_TF(pat, M, TF);
int i, state=0;
for (i = 0; i < N; i++){
state = TF[state][txt[i]];
if (state == M)
printf ("\n Given pattern is found at the index%d",i-M+1);
}
}
int main() {
char *txt = "AABCDAABBDCAABADAABDABAABA";
char *pat = "AABA";
calc_occur(pat, txt);
return 0;
} ผลลัพธ์
Given pattern is found at the index 11 Given pattern is found at the index 22