提交时间:2022-07-19 11:50:59
运行 ID: 52248
#include<bits/stdc++.h> using namespace std; const int MAXN=1000006; char c[MAXN],s[MAXN],t[MAXN]; int kmp[MAXN],n,m,a,b; int gcd(int a,int b) { return !b?a:gcd(b,a%b); } void solve() { scanf("%s%s%d%d",s+1,c+1,&a,&b),n=strlen(s+1),m=strlen(c+1); if(a>b)swap(a,b); for(int i=1; i<=n; ++i)t[i]=s[i]; reverse(t+1,t+a+1),reverse(t+a+1,t+n+1); for(int i=1; i<=n; ++i)s[i+n]=s[i],t[i+n]=t[i]; n<<=1; for(int i=2,j=kmp[1]=0; i<=m; ++i) { while(j&&(c[j+1]!=c[i]))j=kmp[j]; if(c[j+1]==c[i])++j; kmp[i]=j; } int j=0,k=gcd(b-a,m); for(int i=1; i<=n; ++i) { while(j&&c[j+1]!=s[i])j=kmp[j]; if(c[j+1]==s[i])++j; if(j==m) { if((i-m)%k==0)return void(puts("yes")); else j=kmp[j]; } } j=0; for(int i=1; i<=n; ++i) { while(j&&c[j+1]!=t[i])j=kmp[j]; if(c[j+1]==t[i])++j; if(j==m) { if((i-m)%k==0)return void(puts("yes")); else j=kmp[j]; } } puts("no"); } int main() { int T; scanf("%d",&T); while(T--)solve(); return 0; }