-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkmp.cpp
More file actions
43 lines (43 loc) · 914 Bytes
/
kmp.cpp
File metadata and controls
43 lines (43 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n, m;
cin >> n >> m;
vector<int> v1(n), v2(m);
int nxt[m+5] = {};
for (auto &i:v1){
cin >> i;
}
for (auto &i:v2){
cin >> i;
}
int p1 = 0, p2 = -1;
nxt[0] = -1;
while(p1 < m){
if(p2 == -1 || v2[p1] == v2[p2]){
p1++, p2++, nxt[p1] = p2;
}
else{
p2 = nxt[p2];
}
}
p1 = 0, p2 = 0;
while(p1 < n && p2 < m){
if(p2 == -1 || v1[p1] == v2[p2]){
p1++, p2++;
}
else{
p2 = nxt[p2];
}
}
if(p2 == m){
cout << p1 - m + 1 <<endl;
}
else{
cout << -1 << endl;
}
}
}