幾道KMP模板題

2021-10-09 07:47:07 字數 2602 閱讀 1265

記錄下kmp學習過程

注意:這裡的模板s1是文字串s2是匹配串(模式串)

hdu1711 number sequence鏈結

最小匹配位置

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

const ll maxn=

1e6+5;

const ll mod=

1e9+7;

const ll inf=

1e15+5

;int _next[maxn]

;int s1[maxn]

,s2[maxn]

;void

build_next

(int len)

else t=_next[t];}

}int

kmp(

int len1,

int len2)

else ptr2=_next[ptr2];if

(ptr2==len2)

return ptr1-len2+1;

}return-1

;}intmain()

return0;

}

hdu2087 剪花布條鏈結

求匹配次數

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

const ll maxn=

100005

;const ll mod=

1e9+7;

const ll inf=

1e15+5

;int _next[maxn]

;string s1,s2;

//這裡的模板s1是文字串s2是匹配串(模式串)

void

build_next

(int len)

else t=_next[t];}

}int

kmp(

int len1,

int len2)

else ptr2=_next[ptr2];if

(ptr2==len2)

}return cnt;

}int

main()

return0;

}

洛谷p3375 【模板】kmp字串匹配鏈結

這裡與poj3641類似

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

const ll maxn=

100005

;const ll mod=

1e9+7;

const ll inf=

1e15+5

;int _next[maxn]

;string s1,s2;

//這裡的模板s1是文字串s2是匹配串(模式串)

void

build_next

(int len)

else t=_next[t];}

}void

kmp(

int len1,

int len2)

else ptr2=_next[ptr2];if

(ptr2==len2)}}

intmain()

return0;

}

poj oulipo鏈結

#include

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

const ll maxn=

100005

;const ll mod=

1e9+7;

const ll inf=

1e15+5

;int _next[maxn]

;string s1,s2;

//這裡的模板s1是文字串s2是匹配串(模式串)

void

build_next

(int len)

else t=_next[t];}

}int

kmp(

int len1,

int len2)

else ptr2=_next[ptr2];if

(ptr2==len2)

}return cnt;

}int

main()

return0;

}

幾道樹狀陣列的模板題

hdu 1166排兵布陣單點修改 區間查詢的樹狀陣列的應用 1 include2 using namespace std 3 typedef unsigned int ui 4 typedef long long ll 5 typedef unsigned long long ull 6 defin...

KMP模板 和入門題

hdu1711 題意就是給你兩個序列,讓你求b序列在a序列第一次出現 完全相同 的下標 本題就是kmp的模板題,將i指標指向a串,將j指標指向b串,如果匹配就繼續下一位的匹配,如果不匹配,將j跳轉到next j 繼續向前匹配。include define inf 0x3fffffff define ...

幾道趣味題

1.有乙個隨機數發生器,可以產生1到5的隨機數,利用這個隨機發生器,怎樣產生1到7的隨機數 最直接的想法是拿隨機數乘以7然後除以5,但這樣產生的結果並不是等概率的,7 rand 5中產生不了3和6,因而不可行 正確的做法是5 rand rand 然後捨棄最後4個數,剩餘21個數字。每三個數分為一組,...