大話資料結構之串與模式匹配KMP

2021-09-20 07:20:49 字數 1110 閱讀 6984

堅持用blog記錄學習之路,加油

2019.4.29

**陳百強《偏偏喜歡你》

typedef

char string[maxsize+1]

;/* 0號單元存放串的長度 */

// init

status strassign

(string t,

char

*chars)

}//strcopy

status strcopy

(string t,string s)

//empty?

status strempty

(string s)

//strcompare

//clear

//length

//strcat

// substring

//index

//insert

//sredelete

//replace

//樸素模式匹配

intindex

(string s, string t,

int pos)

else

/* 指標後退重新開始匹配 */}if

(j > t[0]

)return i-t[0]

;else

return0;

}//kmp

//getnext

void

get_next

(string t,

int*next)

else

j= next[j]

;/* 若字元不相同,則j值回溯 */}}

//kmp

intindex_kmp

(string s, string t,

int pos)

else

/* 指標後退重新開始匹配 */

j = next[j]

;/* j退回合適的位置,i值不變 */}if

(j > t[0]

)return i-t[0]

;else

return0;

}

大話資料結構 串

1.串的定義 串是由0個或多個字元組成的有限序列,也叫做字串。串中 字元數目n稱為串的長度。子串 串中任意個數的連續字元組成的子串行稱為該字串的子串,包含該子串的串稱為主串。子串中的位置就是該子串第乙個字元在主串中的序號。2.串的比較 計算機的字串標準包括 ascii碼和unicode碼。其中asc...

大話資料結構 串

串 由零個或多個字元組成的有限序列,又名叫字串。操作index的實現演算法 t為非空串。若主串s中第pos個字元之後存在與t相等的子串,則返回第乙個這樣的子串在s中的位置,否則返回0 intindex string s,string t,int pos return0 若無子串與t相等,返回0 用基...

資料結構之串的匹配

串的匹配應用十分廣泛,比如搜尋引擎 拼寫檢查 語言翻譯 資料壓縮等等,都需要進行串的匹配。串的模式匹配設有兩個字串 s 和 t 設 s 為主串 正文串 t 為子串 模式 在主串 s 中查詢與模式 t 相匹配的子串,如果匹配成功,確定相匹配的子串中第乙個字元在主串 s 現位置。下面介紹兩種演算法 bf...