QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#189405#6851. Cyclically Isomorphicspoonjunxi#AC ✓56ms3632kbC++141.7kb2023-09-27 14:21:042023-09-27 14:21:06

Judging History

你现在查看的是最新测评结果

  • [2023-09-27 14:21:06]
  • 评测
  • 测评结果:AC
  • 用时:56ms
  • 内存:3632kb
  • [2023-09-27 14:21:04]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); i++)
#define per(i, j, k) for (int i = (k); i >= (j); i--)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
using VI = std::vector<int>;
using ll = long long;
using PII = std::pair<int, int>;
using db = double;
using namespace std;

using hashv = pair<int, int>;
constexpr int MOD1 = 1000000007, MOD2 = 1000000009;
hashv operator + (hashv a, hashv b) {
	int c1 = a.fi + b.fi, c2 = a.se + b.se;
	if (c1 >= MOD1) c1 -= MOD1;
	if (c2 >= MOD2) c2 -= MOD2;
	return {c1, c2};
}

hashv operator - (hashv a, hashv b) {
	int c1 = a.fi - b.fi, c2 = a.se - b.se;
	if (c1 < 0) c1 += MOD1;
	if (c2 < 0) c2 += MOD2;
	return {c1, c2};
}

hashv operator * (hashv a, hashv b) { 
	return {1ll * a.fi * b.fi % MOD1, 1ll * a.se * b.se % MOD2}; 
}

const int N = 101000;
hashv ps[N], vals[N];
int n, m;

void solve() {
	cin >> n >> m;
	hashv base(13331, 23333);
	rep(i, 1, n) {
		string s;
		cin >> s;
		s = "?" + s;
		ps[m] = make_pair(s[m], s[m]);
		hashv coef = base;
		per(j, 1, m - 1) {
			ps[j] = coef * hashv(s[j], s[j]) + ps[j + 1];
			coef = coef * base;
		}
		hashv q = hashv(0, 0);
		vals[i] = ps[1];
		coef = base;
		rep(j, 2, m) {
			q = q * base + hashv(s[j - 1], s[j - 1]);
			vals[i] = min(vals[i], ps[j] * coef + q); 
			coef = coef * base;
		}
	}
	int q;
	cin >> q;
	rep(i, 1, q) {
		int x, y;
		cin >> x >> y;
		cout << (vals[x] == vals[y] ? "Yes" : "No") << "\n";
	}
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t;
	cin >> t;
	while (t--) {
		solve();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 56ms
memory: 3632kb

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