题目:匹配
方法:没用KMP,用了字符创hash
/************************************************ Author :DarkTong Created Time :2016/8/14 21:53:07 File Name :Hdu_1711.cpp *************************************************/ #include <bits/stdc++.h> using namespace std; typedef unsigned long long ULL; typedef long long LL; const int INF = 0x3f3f3f3f; const double eps = 1e-9; const int maxn = 1100000 + 10; const ULL x = 100007; int s[maxn]; ULL p[maxn], h[maxn]; void init() { p[0]=1; for(int i=1;i<maxn;++i) p[i]=p[i-1]*x; } void Init(int len) { h[len]=0; for(int i=len-1;i>=0;--i) h[i]=h[i+1]*p[1]+s[i]; } ULL Hash(int i, int L) { return h[i]-h[i+L]*p[L]; } int main() { int T, cas=1, n, m; init(); scanf("%d", &T); while(T--) { scanf("%d%d", &n,&m); for(int i=0;i<n;++i) scanf("%d", &s[i]); s[n] = INF; for(int i=n+1;i<n+m+1;++i) scanf("%d", &s[i]); int tn = n+m+1; Init(tn); int ans = -1; for(int i=0;i<n;++i) { if(Hash(i, m)==Hash(n+1, m)) { ans = i+1; break; } } printf("%d\n", ans); } return 0; }
时间: 2024-12-20 16:08:35