题解:
对于询问串, 我们可以从前往后先跑一遍exkmp。
然后在倒过来,从后往前跑一遍exkmp。
我们就可以记录下 对于每个正向匹配来说,最左边的点在哪里。
对于每个反向匹配来说,最右边的点在哪里。
然后判断可不可以构成这个串就好了。
代码:
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 2e5 + 100; int n, z[N]; char s[N]; void init(int n){ z[0] = n; int j = 1, k; for(int i = 1; i < n; i = k){ if(j < i) j = i; while(j < n && s[j] == s[j-i]) j++; z[i] = j-i; k = i+1; while(k + z[k-i] < j) z[k] = z[k-i],k++; } } char ss[N], t[N]; int l[N], r[N]; int q; int main(){ scanf("%s", ss); int lens = strlen(ss); scanf("%d", &q); int ans = 0; while(q--){ scanf("%s", t); int len2 = strlen(t); for(int i = 1; i <= len2; ++i){ l[i] = lens + 1; r[i] = 0; } strcpy(s, t); s[len2] = '@'; strcpy(s+1+len2, ss); init(lens + 1 + len2); for(int i = 1; i <= lens; ++i){ int t = z[len2+i]; l[t] = min(l[t], i); } for(int i = len2-1; i >= 1; --i) l[i] = min(l[i], l[i+1]); reverse(s, s+len2); reverse(s+len2+1, s+lens+1+len2); init(lens + 1 + len2); for(int i = 1; i <= lens; ++i){ int t = z[len2+i]; r[t] = max(r[t], lens-i+1); } for(int i = len2-1; i >= 1; --i) r[i] = max(r[i], r[i+1]); for(int i = 1; i < len2; ++i){ if(l[i] + len2-1 <= r[len2-i]) { ans++; break; } } } cout << ans << endl; return 0; }