提交时间:2022-07-19 11:51:16

运行 ID: 52256

#include<bits/stdc++.h> using namespace std; const int N=5e5+5; char s[2*N],r[N]; char c[N]; bool ok; int vis[2*N]; int len; int nxt[N]; int x,y; void get_next() { nxt[1]=nxt[0]=0; int j=0; for(int i=1;i<len;++i) { while(j!=0&&r[i+1]!=r[j+1])j=nxt[j]; if(r[i+1]==r[j+1])nxt[i+1]=j+1; else nxt[i+1]=j; } return; } void KMP() { int j=0; for(int i=1;i<=2*len;++i) { while(j!=0&&r[j+1]!=s[i])j=nxt[j]; if(r[j+1]==s[i])j++; if(j==len) { int v=i-len+1; vis[v]=1;if(v>len)vis[v-len]=1; ok=true; j=nxt[len]; } } } void solve() { ok=true; memset(vis,0,sizeof(vis)); memset(nxt,0,sizeof(nxt)); if(x>y)swap(x,y); for(int i=len+1;i<=2*len;++i)s[i]=s[i-len]; get_next(); KMP(); if(!ok) { printf("no\n"); return; } int k=y-x; int d=__gcd(len,k); d=len/d; int p=1; while(d--) { p+=k; if(p>len)p-=len; if(vis[p]) { printf("yes\n"); return; } } printf("no\n"); return; } int main() { // freopen("runes.in","r",stdin); // freopen("runes.out","w",stdout); int T; scanf("%d",&T); while(T--) { scanf("%s",s+1); scanf("%s",r+1); len=strlen(s+1); scanf("%d%d",&x,&y); solve(); } return 0; }