QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165591 | #6851. Cyclically Isomorphic | PPP# | AC ✓ | 46ms | 8100kb | C++17 | 2.0kb | 2023-09-05 19:37:28 | 2023-09-05 19:37:28 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n, m;
const int mod[2] = {998244353, (int)1e9 + 7};
int sum(int a, int b, int mod) {
int s = a + b;
if (s >= mod) s -= mod;
return s;
}
int sub(int a, int b, int mod) {
int s = a - b;
if (s < 0) s += mod;
return s;
}
int mult(int a, int b, int mod) {
return (1LL * a * b) % mod;
}
const int pt = 239;
const int maxN = 2e5 + 10;
int pw[2][maxN];
int pref[2][maxN];
pair<int,int> tp[maxN];
void build(string& s, int id) {
string t = s + s;
int n = t.size();
for (int j = 0; j < 2; j++) {
pw[j][0] = 1;
for (int i = 1; i <= n; i++) {
pw[j][i] = mult(pt, pw[j][i - 1], mod[j]);
}
}
for (int j = 0; j < 2; j++) {
pref[j][0] = 0;
for (int i = 1; i <= n; i++) {
pref[j][i] = sum(mult(pt, pref[j][i - 1], mod[j]), (t[i - 1] - 'a' + 1), mod[j]);
}
}
pair<int,int> mn = {INT_MAX, INT_MAX};
for (int st = 0; st < s.size(); st++) {
int D[2] = {0, 0};
for (int z = 0; z < 2; z++) {
D[z] = sub(pref[z][st + s.size()], mult(pw[z][s.size()], pref[z][st], mod[z]), mod[z]);
}
mn = min(mn, make_pair(D[0], D[1]));
}
tp[id] = mn;
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
build(s, i);
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
x--;
y--;
if (tp[x] == tp[y]) {
cout << "Yes\n";
}
else {
cout << "No\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
while (tst--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 46ms
memory: 8100kb
input:
5 10 10000 afcadffafeeebdfedfdafecfdbeecbcadfedcffaedecbacffedeccacfcbfbbdbcefbdefefebcbeacfbaafddbabcbfaeddaaefebbddafaaaaaedcbbdaafaffcbeeebfbbeedebabfbdbaceabfcccfedbdeafccbaffdeadbefbfeadbbedeeabbaafebaceeaeedbaddfecdefaabdbcfadadaebbdeedcfeeabbbdbcefdadbeecafcabbddddbcacadbbcddeddbbeecbdadefbaa...
output:
No No No Yes Yes No No No Yes No No No No No No No No No No No No No Yes No No No No No No No No No No Yes No No No Yes No No No No No No No No Yes No Yes No No No No No Yes No No No No No No No No No Yes No No Yes No No No No No No No No No No No Yes No Yes No Yes No No No No No No No No Yes Yes Ye...
result:
ok 500000 lines