QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#403317#7686. The Phantom Menaceucup-team3215WA 3ms11444kbC++233.8kb2024-05-02 03:37:062024-05-02 03:37:06

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:37:06]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:11444kb
  • [2024-05-02 03:37:06]
  • 提交

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 || re.back())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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 11444kb

input:

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

output:

-1
-1

result:

wrong answer Jury has the answer but participant has not (test case 1)