QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282739#6851. Cyclically IsomorphicieeAC ✓158ms3776kbC++172.6kb2023-12-12 21:33:262023-12-12 21:33:27

Judging History

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

  • [2023-12-12 21:33:27]
  • 评测
  • 测评结果:AC
  • 用时:158ms
  • 内存:3776kb
  • [2023-12-12 21:33:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
struct SuffixArray {
    int _size;
    vector<int> sa;
    string &s;
    SuffixArray(string &str) : _size(str.size()), s(str) {
        s.push_back(0);
        sa.resize(s.size());
        iota(begin(sa), end(sa), 0);
        sort(begin(sa), end(sa),
             [&](int a, int b) { return s[a] == s[b] ? a > b : s[a] < s[b]; });
        vector<int> classes(s.size()), c(s.begin(), s.end()), cnt(s.size());
        for (int len = 1; len < (int)s.size(); len <<= 1) {
            for (int i = 0; i < (int)s.size(); i++) {
                if (i > 0 && c[sa[i - 1]] == c[sa[i]] &&
                    sa[i - 1] + len < (int)s.size() && // enough condition.
                    c[sa[i - 1] + len / 2] == c[sa[i] + len / 2]) {
                    classes[sa[i]] = classes[sa[i - 1]];
                } else {
                    classes[sa[i]] = i;
                }
            }
            iota(begin(cnt), end(cnt), 0);
            copy(begin(sa), end(sa), begin(c));
            for (int i = 0; i < (int)s.size(); i++) {
                int s1 = c[i] - len;
                if (s1 >= 0) sa[cnt[classes[s1]]++] = s1; // for all s1 belong to [n - len + 1, n], they remain unchanged.
            }
            classes.swap(c);
        }
        s.pop_back();
    }

    void output() {
        cout << "SA\tidx\tstr" << endl;
        for (int i = 0; i < size(); i++) {
            cout << i << ": \t" << sa[i] << " \t";
            if (sa[i] != _size)
                cout << s.substr(sa[i], _size - sa[i]) << endl;
            else
                cout << "$" << endl;
        }
        cout << endl;
    }

    int size() const { return _size + 1; }
    int operator[](int k) const { return sa[k]; }
};
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		using ull = unsigned long long;
		constexpr int base = 31;
		vector<ull> H(n);
		for (int t = 0; t < n; t++) {
			string v;
			cin >> v;
			string d = v + v;
			auto sa = SuffixArray(d).sa;
			vector<int> rk(m * 2);
			for (int i = 0; i < m * 2; i++) {
				rk[sa[i + 1]] = i;
			}
			int id = -1, mn = 1e9;
			for (int i = 0; i < m; i++) {
				if (rk[i] < mn) {
					mn = rk[i];
					id = i;
				}
			}
			rotate(v.begin(), v.begin() + id, v.end());
			ull h = 0;
			for (int i = 0; i < m; i++) {
				h = h * base + (v[i] - 'a' + 1);
			}
			H[t] = h;
		}
		int q;
		cin >> q;
		while (q--) {
			int i, j;
			cin >> i >> j;
			i--, j--;
			cout << (H[i] == H[j] ? "Yes" : "No") << "\n";
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 158ms
memory: 3776kb

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