QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#394860#7686. The Phantom Menaceucup-team228#WA 220ms35236kbC++207.3kb2024-04-20 20:48:292024-04-20 20:48:31

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-04-20 20:48:31]
  • 评测
  • 测评结果:WA
  • 用时:220ms
  • 内存:35236kb
  • [2024-04-20 20:48:29]
  • 提交

answer

//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2") // doesn't work on Yandex
//#pragma GCC target("avx") // for old judges
//#pragma GCC target("bmi,bmi2,lzcnt,popcnt") // fast bit operations

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <random>
#include <string>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <bitset>
#include <array>
#include <stack>
#include <sstream>
#include <unordered_set>

using namespace std;
typedef long long ll;

string to_string(string a) { return '"' + a + '"'; }
string to_string(char a) { return "'" + string(1, a) + "'"; }
string to_string(const char* a) { return to_string((string) a); }
string to_string(bool a) { return a ? "true" : "false"; }
template <class T1, class T2>
string to_string(pair<T1, T2> a) {
    return "(" + to_string(a.first) + ", " + to_string(a.second) + ")";
}
template <class T>
string to_string(T a) {
    bool first = true; string res = "{";
    for (const auto& i : a) {
        if (!first) res += ", ";
        first = false;
        res += to_string(i);
    }
    res += "}";
    return res;
}
void debug_out() { cerr << endl; }
template <class T1, class... T2>
void debug_out(T1 a, T2... b) {
    cerr << " " << to_string(a);
    debug_out(b...);
}

#ifdef LOCAL
#define out(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define out(...) 42
#endif

clock_t start_time; void start_timer() { start_time = clock(); }
double get_time() { return (double) (clock() - start_time) / CLOCKS_PER_SEC; }

void Solve();

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
    freopen("usr/share/man/man1/input.txt", "r", stdin);
#endif
    start_timer();
    Solve();
#ifdef LOCAL
    cerr << fixed << setprecision(3);
    cerr << endl << "Time spent: " << get_time() << endl;
#endif
    return 0;
}

// do something, stay focused
// look for stupid bugs
// guess, slow, stress
// don't overgeneralize
// don't rush

// don't waste time on standings

// SOLVE THE PROBLEM OR DIE TRYING
// THE SOLUTION IS ALWAYS SIMPLE
// THE CODE IS ALWAYS SHORT

template <int mod>
struct Modular {
    int val;
    Modular(int _val = 0) : val(_val) {}
    friend Modular operator+(const Modular& a, const Modular& b) {
        return a.val + b.val >= mod ? a.val + b.val - mod : a.val + b.val;
    }
    friend Modular operator-(const Modular& a, const Modular& b) {
        return a.val - b.val < 0 ? a.val - b.val + mod : a.val - b.val;
    }
    friend Modular operator*(const Modular& a, const Modular& b) {
        return int(1ll * a.val * b.val % mod);
    }
};

template <int mod>
struct PolyHash {
    using Mint = Modular<mod>;
    static const int N = 1e6 + 10;
    Mint base = 3;
    Mint pw[N];
    Mint pref[N];
    void init(const string& s) {
        int n = (int) s.size();
        pw[0] = 1;
        pref[0] = 0;
        for (int i = 1; i <= n; i++) {
            pw[i] = pw[i - 1] * base;
            pref[i] = pref[i - 1] * base + (s[i - 1] - 'a');
        }
    }
    Mint get(int l, int r) {
        l++;
        r++;
        return pref[r] - pref[l - 1] * pw[r - l + 1];
    }
};

const int mod1 = 1e9 + 123;
const int mod2 = 1e9 + 321;

PolyHash<mod1> h1[2];
PolyHash<mod2> h2[2];

ll get_hash(int i, int l, int r) {
    ll x = h1[i].get(l, r).val;
    ll y = h2[i].get(l, r).val;
    return (x << 30) | y;
}

vector<pair<string, int>> a[2];
string A[2];
vector<pair<ll, ll>> h[2];
vector<ll> us[2];
vector<ll> vs[2];
vector<vector<pair<int, int>>> g;
vector<int> cycle;
vector<int> ptr;
vector<int> ans[2];

void dfs(int v) {
    if (ptr[v] == g[v].size()) {
        return;
    }
    auto [u, e] = g[v][ptr[v]];
    ptr[v]++;
    dfs(u);
    cycle.push_back(e);
}

bool sol() {
    int n, m;
    cin >> n >> m;
    for (int j = 0; j <= 1; j++) {
        a[j].assign(n, {"", 0});
        A[j].clear();
        for (int i = 0; i < n; i++) {
            cin >> a[j][i].first;
            a[j][i].second = i;
        }
        sort(a[j].begin(), a[j].end());
    }
    bool are_eq = 1;
    for (int i = 0; i < n; i++) {
        if (a[0][i].first != a[1][i].first) {
            are_eq = 0;
        }
        A[0] += a[0][i].first;
        A[1] += a[1][i].first;
    }
    if (are_eq) {
        for (int i = 0; i < n; i++) {
            cout << a[0][i].second + 1 << " ";
        }
        cout << "\n";
        for (int i = 0; i < n; i++) {
            cout << a[1][i].second + 1 << " ";
        }
        cout << "\n";
        return 1;
    }
    for (int j = 0; j <= 1; j++) {
        h1[j].init(A[j]);
        h2[j].init(A[j]);
    }
    for (int shift = 1; shift < m; shift++) {
        for (int j = 0; j <= 1; j++) {
            h[j].assign(n, {0, 0});
            us[j].clear();
            vs[j].clear();
            int s = j == 0 ? shift : m - shift;
            for (int i = 0; i < n; i++) {
                ll h_u = get_hash(j, i * m, i * m + s - 1);
                ll h_v = get_hash(j, i * m + s, (i + 1) * m - 1);
                h[j][i] = {h_u, h_v};
                if (j == 1) {
                    swap(h_u, h_v);
                }
                us[j].push_back(h_u);
                vs[j].push_back(h_v);
            }
            sort(us[j].begin(), us[j].end());
            sort(vs[j].begin(), vs[j].end());
        }
        if ((us[0] != us[1]) || (vs[0] != vs[1])) {
            continue;
        }
        us[0].erase(unique(us[0].begin(), us[0].end()), us[0].end());
        vs[0].erase(unique(vs[0].begin(), vs[0].end()), vs[0].end());
        int cnt_us = (int) us[0].size();
        int cnt_vs = (int) vs[0].size();
        g.assign(cnt_us + cnt_vs, {});
        for (int j = 0; j <= 1; j++) {
            for (int i = 0; i < n; i++) {
                auto [h_u, h_v] = h[j][i];
                if (j == 1) swap(h_u, h_v);
                int u = (int) (lower_bound(us[0].begin(), us[0].end(), h_u) - us[0].begin());
                int v = (int) (lower_bound(vs[0].begin(), vs[0].end(), h_v) - vs[0].begin());
                if (j == 1) swap(u, v);
                if (j == 0) {
                    v += cnt_us;
                }
                else {
                    u += cnt_us;
                }
                g[u].push_back({v, a[j][i].second});
            }
        }
        cycle.clear();
        ptr.assign(g.size(), 0);
        dfs(0);
        if (cycle.size() != 2 * n) {
            continue;
        }
        reverse(cycle.begin(), cycle.end());
        ans[0].clear();
        ans[1].clear();
        for (int i = 0; i < 2 * n; i++) {
            ans[i % 2].push_back(cycle[i]);
        }
        for (int i : ans[0]) {
            cout << i + 1 << " ";
        }
        cout << "\n";
        for (int i : ans[1]) {
            cout << i + 1 << " ";
        }
        cout << "\n";
        return 1;
    }
    return 0;
}

void Solve() {
    int T;
    cin >> T;
    while (T--) {
        bool ans = sol();
        if (!ans) {
            cout << "-1\n";
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 35032kb

input:

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

output:

1 3 2 
1 2 3 
-1

result:

ok 2 cases (2 test cases)

Test #2:

score: 0
Accepted
time: 220ms
memory: 35236kb

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

result:

ok 500000 cases (500000 test cases)

Test #4:

score: 0
Accepted
time: 146ms
memory: 35232kb

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

output:

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

result:

ok 500000 cases (500000 test cases)

Test #5:

score: -100
Wrong Answer
time: 97ms
memory: 35004kb

input:

333333
1 3
cbb
bfa
1 3
bbb
bfa
1 3
abb
bfa
1 3
fab
bfa
1 3
eab
bfa
1 3
dab
bfa
1 3
cab
bfa
1 3
bab
bfa
1 3
aab
bfa
1 3
ffa
bfa
1 3
efa
bfa
1 3
dfa
bfa
1 3
cfa
bfa
1 3
bfa
bfa
1 3
afa
bfa
1 3
fea
bfa
1 3
eea
bfa
1 3
dea
bfa
1 3
cea
bfa
1 3
bea
bfa
1 3
aea
bfa
1 3
fda
bfa
1 3
eda
bfa
1 3
dda
bfa
1 3
c...

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:

wrong answer not cyclic isomorphism (test case 183)