You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
855 B
C++
33 lines
855 B
C++
#include <cstdio>
|
|
#include <cstring>
|
|
const int N = 1000050;
|
|
int a[N], b[N], nxt[N];
|
|
int main()
|
|
{
|
|
nxt[0] = -1;
|
|
int T, n, m;
|
|
scanf("%d", &T);
|
|
while (T--)
|
|
{
|
|
scanf("%d%d", &n, &m);
|
|
for (int i = 0; i < n; i++)
|
|
scanf("%d", a + i);
|
|
for (int i = 0; i < m; i++)
|
|
scanf("%d", b + i);
|
|
for (int i = 1, j = -1; i < m; i++)
|
|
{
|
|
while (~j && b[j + 1] != b[i]) j = nxt[j];
|
|
if (b[j + 1] == b[i]) j++;
|
|
nxt[i] = j;
|
|
}
|
|
int ans = -1;
|
|
for (int i = 0, j = -1; i < n && ans == -1; i++)
|
|
{
|
|
while (~j && b[j + 1] != a[i]) j = nxt[j];
|
|
if (b[j + 1] == a[i]) j++;
|
|
if (j == m - 1) ans = i - m + 2, j = nxt[j];
|
|
}
|
|
printf("%d\n", ans);
|
|
}
|
|
return 0;
|
|
} |