提交时间:2022-07-19 11:52:06
运行 ID: 52309
#include<bits/stdc++.h> using namespace std; #define ull unsigned long long const int N=5e5+10,P=131; int T,a,b,n; string s,r,s1,s2; int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } ull hsh[N<<1],hsh1[N<<1],hsh2[N<<1]; ull rhsh,p[N<<1]; ull Findhsh(ull h[],int l,int r){ return h[r]-h[l-1]*p[r-l+1]; } int main(){ cin>>T; while(T--){ bool flag=0; s=s1=s2=r=""; cin>>s>>r>>a>>b; n=s.size(); if(s.size()!=r.size()){ puts("no"); continue; } int d=gcd(max(a,b)-min(a,b),n); //printf("%d\n",d); s1=s.substr(a)+s.substr(0,a); s2=s.substr(b)+s.substr(0,b); reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); //cout<<s1<<" "<<s2<<endl; rhsh=hsh[0]=hsh1[0]=hsh2[0]=0,p[0]=1; s=","+s+s,s1=","+s1+s1,s2=","+s2+s2,r=","+r; for(register int i=1;i<=2*n;i++){ p[i]=(ull)p[i-1]*P; hsh[i]=(ull)hsh[i-1]*P+s[i]-'a'+1; hsh1[i]=(ull)hsh1[i-1]*P+s1[i]-'a'+1; hsh2[i]=(ull)hsh2[i-1]*P+s2[i]-'a'+1; if(i<=n) rhsh=(ull)rhsh*P+r[i]-'a'+1; } int now=1; for(register int i=1;i<=n;i++){ if(Findhsh(hsh,now,now+n-1)==rhsh){ flag=1; break; } if(Findhsh(hsh1,now,now+n-1)==rhsh){ flag=1; break; } if(Findhsh(hsh2,now,now+n-1)==rhsh){ flag=1; break; } now=(now+n-d)%(n+1); if(now>n) now++; } if(flag){ puts("yes"); continue; } now=1; for(register int i=1;i<=n;i++){ if(Findhsh(hsh,now,now+n-1)==rhsh){ flag=1; break; } if(Findhsh(hsh1,now,now+n-1)==rhsh){ flag=1; break; } if(Findhsh(hsh2,now,now+n-1)==rhsh){ flag=1; break; } now=(now+d)%(n+1); if(now>n) now++; } if(flag){ puts("yes"); continue; } puts("no"); } return 0; } /* 3 ljhelloh hellohlj 2 4 thisisastr htrtsasisi 3 5 abcde bcdea 1 4 */