串,包括KMP演算法(C C )

2021-07-02 01:08:49 字數 3114 閱讀 2957

#ifndef stringadt_h_

#define stringadt_h_

#include #define folwover -1

struct stringadt;

typedef struct stringadt string;

struct stringadt

;//為s開闢合適空間,儲存輸入的chars

void strassign(string &s, char * chars);

//利用s2覆蓋s1

void strcopy(string &s1, string &s2);

//串空返回true

bool strisempty(string &s);

//若s1>s2,返回1,以此類推

int stringcompare(string &s1, string &s2);

//返回字串的長度

int strlength(string &s);

//清空串s

void clearstring(string &s);

//將s2接在s1後面,賦值給t

void strconcat(string &t, string &s1, string &s2);

//將s第pos個字元起,共len個字元另外組成乙個字串賦值給sub

void substring(string &sub, string &s, int pos, int len);

//kmp演算法,從第pos個字元算起,若t是s的子串,返回t第一次出現的位置

int index(string &s, string &t, int pos);

//若t是s的子串,用v替代所有的t

void replace(string &s, string &t, string &v);

//在串s的第pos個字元之前插入串t

void strinsert(string &s, int pos, string &t);

//刪除串s第pos個字元開始長度為len的字串

void strdelete(string &s, int pos, int len);

//列印出string,包括回車

void printfstring(string &s);

#endif

#include #include #include #include "stringadt.h"

void strassign(string &s, char * chars)

s.ch = (char *)malloc(lenchars * sizeof(char));//為字串分配記憶體,這裡不多開闢乙個空間給'\0'

if (s.ch == null) //是因為有length來替代'\0'指示字串的結尾

exit(folwover);

chars = chars - lenchars; //指標指回字串的第乙個值

s.length = lenchars;

for (i = 0; i < lenchars; i++)

s.ch[i] = chars[i];

}void strcopy(string &s1, string &s2)

s1.length = s2.length;

}bool strisempty(string &s)

int stringcompare(string &s1, string &s2)

else if (s1.ch[i] < s2.ch[i])

else

i++;

} return result;

}int strlength(string &s)

void clearstring(string &s)

void strconcat(string &t, string &s1, string &s2)

for (int i = s1.length, j = 0; i < s2.length + s1.length; i++, j++)

t.length = s1.length + s2.length;

}void substring(string &sub, string &s, int pos, int len)

else }}

void nextinit(string &t, int next)//檢測無誤

else }}

int index(string &s, string &t, int pos)//驗證無誤

next = (int *)malloc(sizeof(int) * t.length);

nextinit(t, next);//初始化next陣列

while (j < s.length)

i++;

j++;

} else

} return index;//匹配成功返回下標值,失敗則返回-1

}void replace(string &s, string &t, string &v)

else }}

void strinsert(string &s, int pos, string &t)//驗證無誤

for (int i = pos - 1, j = 0; j < t.length; i++, j++)

s.length = s.length + t.length;

}void strdelete(string &s, int pos, int len)//驗證無誤

else

for (int i = pos + len - 1, j = pos - 1; i < s.length; i++, j++)

free(s.ch);

s.ch = tempchararray;

s.length = s.length - len; }}

void printfstring(string &s)

printf("\n");

}

#include #include #include "stringadt.h"

int main()

KMP演算法C c 實現

直接上 using system using system.collections.generic using system.text namespace dotnetkmp class program public static int strnext 失效函式值 static void main...

串 KMP演算法

一 串 1 串是由 0個或者多個字元構成的有限序列 2 字典序 定義字串的大小也稱為字典序。1 若兩個串長度為 m,n。m n,則m 串大於n串 2 若m n 當兩串,第乙個不同的字元,哪個大的哪個串就大 二 串的儲存 1 順序儲存 定長儲存串,放在陣列中進行操作。當溢位了後會自動截斷 2 堆分配儲...

KMP串匹配演算法

串的模式匹配是串處理系統中的最重要操作之一,普通匹配演算法的時間複雜度為o m n 然而,kmp演算法的演算法時間複雜度為o m n 其主要改進是 當出現比較不等時,不需回溯指標,而是利用已經得到的部分匹配的結果將模式向後滑動盡可能遠的距離。kmp演算法的本質是基於有限自動機理論,它簡化了有限自動機...