http://acm.hdu.edu.cn/showproblem.php?pid=1711
#include<iostream> using namespace std; int nx[10000+5], b[10000+5], a[1000000+5]; int n, m; void getnext() { int k = -1; nx[0] = -1; int j = 0; while(j < m) { if(k == -1 || b[j] == b[k]) nx[++j] = ++k; else k = nx[k]; } } int KMP() { int flag = -1; for(int i = 0, j = 0; i < n; i++) { while(j > 0 && b[j] != a[i]) j = nx[j]; if(a[i] == b[j]) j++; if(j == m) { return i - j + 2; } } return -1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--) { cin >> n >> m; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < m; i++) cin >> b[i]; getnext(); cout << KMP() << endl; } return 0; }