QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#279534#6851. Cyclically IsomorphicTaylorFlyAC ✓131ms8488kbC++201.4kb2023-12-08 20:57:382023-12-08 20:57:39

Judging History

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

  • [2023-12-08 20:57:39]
  • 评测
  • 测评结果:AC
  • 用时:131ms
  • 内存:8488kb
  • [2023-12-08 20:57:38]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()

using i64 = long long;

using ULL = unsigned long long;
constexpr int P = 131;
struct Hash{
    static std::vector<ULL> h;
    std::vector<ULL> a;
    int n;
    Hash():n(0), a(0){};
    Hash(std::string s) {
        init(s);
    }
    void init(std::string s) {
        n = (int)s.size();
        a.resize(n + 1);
        while (h.size() < n) {
            h.push_back(h.back() * P);
        }
        for (int i = 0; i < n; i++) {
            a[i + 1] = a[i] * P + s[i];
        }
    }
    ULL range(int l, int r) {
        return a[r + 1] - a[l] * h[r - l + 1];
    }
    ULL shift(int d) {
        d %= n;
        return range(d, n - 1) * h[d] + range(0, d - 1);
    }
};
std::vector<ULL> Hash::h{1};

void solve() {
    int n, m;
    std::cin >> n >> m;
    std::vector<std::set<ULL>> a(n);
    for (int i = 0; i < n; i++) {
        std::string s;
        std::cin >> s;
        Hash hash(s);
        for (int j = 0; j < m; j++) {
            a[i].insert(hash.shift(j));
        }
    }
    int q;
    std::cin >> q;
    while (q--) {
        int i, j;
        std::cin >> i >> j;
        i--, j--;
        std::cout << (a[i].count(*a[j].begin()) ? "Yes" : "No") << "\n";
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--)
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 131ms
memory: 8488kb

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