QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#238629#7686. The Phantom Menaceucup-team1951#WA 1197ms11148kbC++204.8kb2023-11-04 17:09:452023-11-04 17:09:45

Judging History

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

  • [2024-10-08 14:11:03]
  • hack成功,自动添加数据
  • (/hack/941)
  • [2024-10-08 10:05:28]
  • hack成功,自动添加数据
  • (/hack/940)
  • [2024-10-07 19:51:15]
  • hack成功,自动添加数据
  • (/hack/938)
  • [2024-10-07 19:28:01]
  • hack成功,自动添加数据
  • (/hack/937)
  • [2024-10-07 17:16:32]
  • hack成功,自动添加数据
  • (/hack/936)
  • [2024-10-07 16:53:09]
  • hack成功,自动添加数据
  • (/hack/935)
  • [2024-10-07 16:22:17]
  • hack成功,自动添加数据
  • (/hack/934)
  • [2023-11-04 17:09:45]
  • 评测
  • 测评结果:WA
  • 用时:1197ms
  • 内存:11148kb
  • [2023-11-04 17:09:45]
  • 提交

answer

#include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_map>

using ll = long long;
using ull = unsigned long long;
using u128 = __uint128_t;

constexpr ull mod = (1ULL << 61) - 1;
constexpr ull base = 314159265ULL;
std::vector<ull> base_pow;

inline ull add(ull a, ull b) {
    a += b;
    if (a >= mod) a -= mod;
    return a;
}

inline ull mul(ull a, ull b) {
    u128 t = (u128)a * b;
    ull na = t >> 61;
    ull nb = t & mod;
    return add(na, nb);
    // return (u128)a * b % mod;
}

void init_base_pow() {
    int max_len = 1e6 + 10;
    base_pow.resize(max_len);
    base_pow[0] = 1;

    for (int i = 1; i < max_len; i++) {
        base_pow[i] = base_pow[i - 1] * base % mod;
    }
}

struct Hash {
    int n;
    std::vector<ull> h;

    Hash(std::string &s) {
        n = s.size();
        h.resize(n + 1);
        for (int i = 0; i < n; i++) {
            h[i + 1] = add(mul(h[i], base), s[i]);
        }
    }

    ull get(int l, int r) {
        return add(h[r], mod - mul(h[l], base_pow[r - l]));
    }
};

// struct UF {
//     std::unordered_map<ull, std::tuple<ull, int, int>> uf; // parent, size, count

//     UF() {}

//     void add(ull x) {
//         if (!uf.contains(x)) uf[x] = {x, 1, 0};
//     }

//     ull find(ull x) {
//         if (std::get<0>(uf[x]) == x) return x;
//         return std::get<0>(uf[x]) = find(std::get<0>(uf[x]));
//     }

//     void unite(ull x, ull y) {
//         add(x);
//         add(y);
//         std::get<2>(uf[x])++;
//         std::get<2>(uf[y])++;

//         x = find(x);
//         y = find(y);
//         if (x == y) return;
//         if (std::get<1>(uf[x]) < std::get<1>(uf[y])) std::swap(x, y);
//         std::get<0>(uf[y]) = x;
//         std::get<1>(uf[x]) += std::get<1>(uf[y]);
//     }
// };

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<Hash> hs, ht;
    for (int i = 0; i < n; i++) {
        std::string s;
        std::cin >> s;
        hs.emplace_back(s);
    }
    for (int i = 0; i < n; i++) {
        std::string t;
        std::cin >> t;
        ht.emplace_back(t);
    }

    for (int i = 0; i <= m; i++) {
        std::unordered_map<ull, std::vector<std::pair<ull, int>>> graph;
        std::unordered_map<ull, int> in_count;
        int j = 0;
        for (Hash &h: hs) {
            ull a = h.get(0, i) * 2;
            ull b = h.get(i, m) * 2 + 1;
            // std::cout << "hashs " << "i " << i << " " << a << " " << b << "\n";
            graph[a].emplace_back(b, j++);
            in_count[b]++;
        }
        for (Hash &h: ht) {
            ull a = h.get(0, m - i) * 2 + 1;
            ull b = h.get(m - i, m) * 2;
            // std::cout << "hasht " << "i " << i << " " << a/ << " " << b << "\n";
            graph[a].emplace_back(b, j++);
            in_count[b]++;
        }

        bool ok = true;
        for (auto &[x, cnt]: in_count) {
            if (graph[x].size() != cnt) {
                // std::cout << "test_ " << i << " " << x << " " << graph[x].size() << " " << cnt << "\n";
                ok = false;
                break;
            }
        }
        if (!ok) {
            // std::cout << "test " << i << "\n";
            continue;
        }

        ull from = graph.begin()->first;

        std::vector<int> path, back_path;

        // construct euler path
        auto dfs = [&](auto &&self, ull x) -> void {
            if (graph[x].empty()) {
                if (x != from) {
                    path.insert(path.end(), back_path.begin(), back_path.end());
                    back_path.clear();
                }
            } else {
                while (!graph[x].empty()) {
                    auto [y, id] = graph[x].back();
                    graph[x].pop_back();
                    back_path.push_back(id);
                    self(self, y);
                }
            }
        };
        dfs(dfs, from);
        path.insert(path.end(), back_path.begin(), back_path.end());


        if (path.size() != 2 * n) {
            // std::cout << "test2 " << i << "\n";
            continue;
        }

        // std::reverse(path.begin(), path.end());

        std::vector<int> a, b;
        for (int e: path) {
            if (e < n) {
                a.push_back(e);
            } else {
                b.push_back(e - n);
            }
        }

        for (int j: a) {
            std::cout << j + 1 << " ";
        }
        std::cout << "\n";
        for (int j: b) {
            std::cout << j + 1 << " ";
        }
        std::cout << "\n";
        return;
    }

    std::cout << "-1\n";
}


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

    init_base_pow();

    int t;
    std::cin >> t;
    while (t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 10812kb

input:

2
3 3
abc
ghi
def
bcd
efg
hia
1 3
abc
def

output:

1 3 2 
3 1 2 
-1

result:

ok 2 cases (2 test cases)

Test #2:

score: 0
Accepted
time: 1197ms
memory: 11112kb

input:

1000000
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
...

output:

1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
...

result:

ok 1000000 cases (1000000 test cases)

Test #3:

score: 0
Accepted
time: 662ms
memory: 11064kb

input:

500000
1 2
dd
ba
1 2
cd
ba
1 2
bd
ba
1 2
ad
ba
1 2
dc
ba
1 2
cc
ba
1 2
bc
ba
1 2
ac
ba
1 2
db
ba
1 2
cb
ba
1 2
bb
ba
1 2
ab
ba
1 2
da
ba
1 2
ca
ba
1 2
ba
ba
1 2
aa
ba
1 2
dd
aa
1 2
cd
aa
1 2
bd
aa
1 2
ad
aa
1 2
dc
aa
1 2
cc
aa
1 2
bc
aa
1 2
ac
aa
1 2
db
aa
1 2
cb
aa
1 2
bb
aa
1 2
ab
aa
1 2
da
aa
1 2...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
-1
-1
-1
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 500000 cases (500000 test cases)

Test #4:

score: 0
Accepted
time: 800ms
memory: 11148kb

input:

500000
2 1
d
d
b
a
2 1
c
d
b
a
2 1
b
d
b
a
2 1
a
d
b
a
2 1
d
c
b
a
2 1
c
c
b
a
2 1
b
c
b
a
2 1
a
c
b
a
2 1
d
b
b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
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 333333 cases (333333 test cases)

Test #6:

score: -100
Wrong Answer
time: 693ms
memory: 11064kb

input:

333333
3 1
c
b
b
b
f
a
3 1
b
b
b
b
f
a
3 1
a
b
b
b
f
a
3 1
f
a
b
b
f
a
3 1
e
a
b
b
f
a
3 1
d
a
b
b
f
a
3 1
c
a
b
b
f
a
3 1
b
a
b
b
f
a
3 1
a
a
b
b
f
a
3 1
f
f
a
b
f
a
3 1
e
f
a
b
f
a
3 1
d
f
a
b
f
a
3 1
c
f
a
b
f
a
3 1
b
f
a
b
f
a
3 1
a
f
a
b
f
a
3 1
f
e
a
b
f
a
3 1
e
e
a
b
f
a
3 1
d
e
a
b
f
a
3 1
c...

output:

-1
-1
-1
3 2 1 
2 1 3 
-1
-1
-1
-1
-1
-1
-1
-1
-1
3 2 1 
2 3 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
3 2 1 
2 3 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
3 2 1 
2 3 1 
-1
-1
-1
-1
-...

result:

wrong answer not cyclic isomorphism (test case 14)