提交时间:2024-01-03 13:44:49
运行 ID: 119038
#include<bits/stdc++.h> using namespace std; const int MXN = 500005,RDX = 26,MOD1 = 998244353,MOD2 = 1000000007; const char DIFF = 'a'; int thsv1, thsv2, chsv1, chsv2; char tgt[MXN], cur[MXN]; int gcd(int a, int b) { while(b){ int temp(b); b = a % b; a = temp; } return a; } int pw(int b, int p, int m) { int ans(1); while(p){ if(p & 1)ans = ans * 1LL * b % m; b = b * 1LL * b % m; p >>= 1; } return ans % m; } int strhs(char str[], int len, int mod) { int ans(0); for(int i(0); i != len; ++i) ans = (ans * 1LL * RDX % mod + (str[i] - DIFF)) % mod; return ans; } int main() { int T(0); cin >> T; while(T--){ int A(0), B(0); cin >> cur >> tgt >> A >> B; bool able(false); if(A < B)swap(A, B); int N = strlen(tgt),L = gcd(N, A - B),pw1(pw(RDX, N - 1, MOD1)), pw2(pw(RDX, N - 1, MOD2)); thsv1 = strhs(tgt, N, MOD1), thsv2 = strhs(tgt, N, MOD2); chsv1 = strhs(cur, N, MOD1), chsv2 = strhs(cur, N, MOD2); for(int i(0); !able && i != N; ++i){ if(i % L == 0 && chsv1 == thsv1 && chsv2 == thsv2)able = true; chsv1 = ((chsv1 - (cur[i] - DIFF) * 1LL * pw1 % MOD1 + MOD1) % MOD1* 1LL * RDX % MOD1+ (cur[i] - DIFF)) % MOD1; chsv2 = ((chsv2 - (cur[i] - DIFF) * 1LL * pw2 % MOD2 + MOD2) % MOD2* 1LL * RDX % MOD2+ (cur[i] - DIFF)) % MOD2; } for(int i(0); i < A - i - 1; ++i) swap(tgt[i], tgt[A - i - 1]); for(int i(A); i < N - (i - A) - 1; ++i) swap(tgt[i], tgt[N - (i - A) - 1]); thsv1 = strhs(tgt, N, MOD1), thsv2 = strhs(tgt, N, MOD2); chsv1 = strhs(cur, N, MOD1), chsv2 = strhs(cur, N, MOD2); for(int i(0); !able && i != N; ++i){ if(i % L == 0 && chsv1 == thsv1 && chsv2 == thsv2)able = true; chsv1 = ((chsv1 - (cur[i] - DIFF) * 1LL * pw1 % MOD1 + MOD1) % MOD1* 1LL * RDX % MOD1+ (cur[i] - DIFF)) % MOD1; chsv2 = ((chsv2 - (cur[i] - DIFF) * 1LL * pw2 % MOD2 + MOD2) % MOD2* 1LL * RDX % MOD2+ (cur[i] - DIFF)) % MOD2; } puts(able ? "yes" : "no"); } return 0; }