KMP演算法(模板)

2021-06-22 14:01:13 字數 614 閱讀 6085

time limit: 1000ms   memory limit: 65536k  有疑問?點這裡^_^

給定兩個字串string1和string2,判斷string2是否為string1的子串。

輸入包含多組資料,每組測試資料報含兩行,第一行代表string1(長度小於1000000),第二行代表string2(長度小於1000000),string1和string2中保證不出現空格。

對於每組輸入資料,若string2是string1的子串,則輸出string2在string1中的位置,若不是,輸出-1。

abc

a123456

45abc

ddd

1

4-1

cjx模板。

#include #include #include #include #include using namespace std;

char s[1000010];

char t[1000010];

int next[1000010];

void get_next()

int main()

KMP演算法模板

在文字t 1.n 中找到某個模式p 1.m 所有出現的位置被稱作字串匹配問題 m n p3375 模板 kmp字串匹配 這道題在洛谷上的評級居然是普及 普及?qvq 實現起來還是比較簡單的,只不過有很多細節可以有很多種寫法,看別人的 容易凌亂。理解起來比較困難的部分是如何get next 不建議初學...

kmp演算法模板

字串匹配樸素演算法 傳統的字串匹配 效率較低 計算主串中模式串出現的次數 include include using namespace std int index string s,string t else if j lent return count int main kmp演算法 下標從零開...

KMP演算法 模板

include include include include include include using namespace std const int maxn 100005 char t maxn p maxn int lent,lenp int next maxn int h 0 陣列從0開...