QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#403315#7686. The Phantom Menaceucup-team3215WA 632ms11640kbC++233.8kb2024-05-02 03:29:462024-05-02 03:29:47

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)
  • [2024-05-02 03:29:47]
  • 评测
  • 测评结果:WA
  • 用时:632ms
  • 内存:11640kb
  • [2024-05-02 03:29:46]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 1e6 + 123;
constexpr uint64_t T = -5ull / 2;
array<uint64_t, 26> remap;
array<uint64_t, N> power;

mt19937_64 rng{1000 - 7};

#define sz(x) ssize(x)

using vi = vector<int>;
using pii = pair<int, int>;

vi eulerWalk(vector<vector<pii>> &gr, int nedges, int src = 0) {
    int n = sz(gr);
    vi D(n), its(n), eu(nedges), ret, s = {src};
    D[src]++; // to allow Euler paths, not just cycles
    while (!s.empty()) {
        int x = s.back(), y, e, &it = its[x], end = sz(gr[x]);
        if (it == end) {
            ret.push_back(x);
            s.pop_back();
            continue;
        }
        tie(y, e) = gr[x][it++];
        if (!eu[e]) {
            D[x]--, D[y]++;
            eu[e] = 1;
            s.push_back(y);
        }
    }
    for (int x: D) if (x < 0 || sz(ret) != nedges + 1) return {};
    return {ret.rbegin(), ret.rend()};
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<string> a(n), b(n);
    for (auto &i: a)cin >> i;
    for (auto &i: b)cin >> i;
    vector<vector<uint64_t>> ha(n), hb(n);
    for (int i = 0; i < n; ++i) {
        uint64_t cur{0};
        ha[i].push_back(0);
        for (auto &c: a[i]) {
            cur = cur * T + remap[c - 'a'];
            ha[i].push_back(cur);
        }
        cur = 0;
        hb[i].push_back(0);
        for (auto &c: b[i]) {
            cur = cur * T + remap[c - 'a'];
            hb[i].push_back(cur);
        }
    }
    for (int sh = 0; sh < m; ++sh) {
        map<uint64_t, uint64_t> s, p;
        for (int i = 0; i < n; ++i) {
            uint64_t pref = hb[i][sh];
            uint64_t suff = hb[i].back() - pref * power[m - sh];
            if (!s.count(suff))s[suff] = s.size();
            if (!p.count(pref))p[pref] = p.size();
        }
        bool ok = true;
        int e = 0;
        map<pair<uint64_t, uint64_t>, vector<uint64_t>> whoa, whob;
        vector<vector<pii>> g(s.size() + p.size());
        for (int i = 0; i < n; ++i) {
            uint64_t pref = hb[i][sh];
            uint64_t suff = hb[i].back() - pref * power[m - sh];
            whob[{s.size() + p[pref], s[suff]}].push_back(i);
            g[s.size() + p[pref]].push_back({s[suff], e++});
        }
        for (int i = 0; i < n; ++i) {
            uint64_t suff = ha[i][m - sh];
            uint64_t pref = ha[i].back() - suff * power[sh];
            if (!p.count(pref) || !s.count(suff)) {
                ok = false;
                continue;
            }
            whoa[{s[suff], s.size() + p[pref]}].push_back(i);
            g[s[suff]].push_back({s.size() + p[pref], e++});
        }
        if (!ok) {
            continue;
        }
        auto re = eulerWalk(g, e);
        if (re.size() < e)continue;
//        cout << re.size() << endl;
        re.pop_back();
        vector<uint64_t> resa, resb;
        for (int i = 0; i + 1 < re.size(); i += 2) {
            auto &v = whoa[{re[i], re[i + 1]}];
            resa.push_back(v.back());
            v.pop_back();
        }
        resb.push_back(0);
        for (int i = 1; i + 1 < re.size(); i += 2) {
            auto &v = whob[{re[i], re[i + 1]}];
            resb.push_back(v.back());
            v.pop_back();
        }
        for (auto x: resa) {
            cout << x + 1 << " ";
        }
        cout << "\n";
        for (auto x: resb) {
            cout << x + 1 << " ";
        }
        cout << "\n";
        return;
    }
    cout << "-1\n";
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    for (auto &i: remap) i = rng();
    power[0] = 1;
    for (int i = 1; i < N; ++i)power[i] = power[i - 1] * T;
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

// defghiabc
// bcdefghia

详细

Test #1:

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

input:

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

output:

3 2 1 
1 2 3 
-1

result:

ok 2 cases (2 test cases)

Test #2:

score: 0
Accepted
time: 632ms
memory: 11332kb

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: 313ms
memory: 11640kb

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
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1 
1 2 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 2 
1 2 
-1
-1
2 1 
1 2 
2 1 
1 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 2 
1 2 
-1
-1
-1
-1
-1
2 1 
1 2 
-1
2 1 
1 1...

result:

wrong answer q is not permutation (test case 11)