#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define debug cout<<"debug"<<endl
mt19937 rd(time(0));
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
const double eps = 1e-8;
const double PI = 3.14159265358979323;
const int N = 110, M = 2*N, mod = 998244353;
const int INF = 0x3f3f3f3f;
int n;
// 字符串最小表示法 求起点位置p
// smin = s.substr(p) + s.substr(0, p);
int minstr(string &s)
{
int i = 0, j = 1, n = s.size();
for(int k = 0; i<n && j<n && k<n; )
{
if(s[(i+k)%n] < s[(j+k)%n])
{
j += k+1;
k = 0;
if(i == j) j++;
}else if(s[(i+k)%n] > s[(j+k)%n])
{
i += k+1;
k = 0;
if(i == j) j++;
}else k++;
}
return min(i, j);
}
void solve()
{
int n, m;
cin>>n>>m;
vector<string> s(n);
for(int i = 0; i<n; i++)
{
cin>>s[i];
int p = minstr(s[i]);
s[i] = s[i].substr(p) + s[i].substr(0, p);
}
int q; cin>>q;
while(q -- )
{
int x, y; cin>>x>>y;
x--, y--;
cout<<(s[x] == s[y] ? "Yes" : "No")<<endl;
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int T;
T = 1;
cin>>T;
while(T -- )
{
solve();
}
return 0;
}
23杭电(一)-E-Cyclically Isomorphic(字符串最小表示法)
最新推荐文章于 2024-07-15 22:10:26 发布