QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404506#7686. The Phantom Menaceucup-team3215RE 0ms0kbC++204.1kb2024-05-04 01:39:422024-05-04 01:39:44

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-04 01:39:44]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-05-04 01:39:42]
  • 提交

answer

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

using namespace std;

constexpr uint32_t N = 1e6 + 1, mod = -37u / 2;

vector<int> todo, q;
uint64_t head[N], tail[N];
unordered_map<uint64_t, vector<int>> byh;

uint32_t pw[N / 2 + 1], remap[26][2], h[2][N][2];

int nx[N / 2 + 1];
unordered_map<uint64_t, vector<uint64_t>> to;

uint64_t hget(int x, int l, int n) { return (h[x][l][0] * (uint64_t)pw[n] + mod - h[x][l + n][0]) % mod << 31 |
                                            (h[x][l][1] * (uint64_t)pw[n] + mod - h[x][l + n][1]) % mod; }

bool extend(int v) {
  int skipped = 0;
  for (auto it = to[tail[v]].end(); it != to[tail[v]].begin(); ) {
    auto x = *--it;
    while (byh[x].size()) {
      auto y = byh[x].back(); byh[x].pop_back();
      to[tail[v]].erase(it);
      todo.push_back(y);
      return extend(nx[v] = y);
    }
    if (skipped++) return 0;
  }
  return 1;
}

void solve() {
  int n, m; cin >> n >> m;
  string s[2];
  for (int i = 0; i < 2 * n; ++i) { string c; cin >> c, s[i >= n] += c; }
  if (n == 1) {
    bool ok = 0;
    int h[2][2]{};
    for (int x: {0, 1}) for (int y: {0, 1})
    for (int i = 0; i < n * m; ++i) h[x][y] = (h[x][y] * 3ull + remap[s[x][i] - 'a'][y]) % mod;
    int p = 1;
    for (int i = 0; i < n * m; ++i) p = p * 3ull % mod;
    p = mod + 1 - p;
    for (int i = 0; i < n * m; ++i) {
      ok |= h[0][0] == h[1][0] && h[0][1] == h[1][1];
      for (int y: {0, 1}) h[1][y] = (h[1][y] * 3ull + remap[s[1][i] - 'a'][y] * (uint64_t)p) % mod;
    }
    cout << (ok? "1\n1\n": "-1\n");
    return;
  }
  if (m == 1) {
    int cnts[2][26]{};
    for (int i: {0, 1})
    for (int j = 0; j < n; ++j) ++cnts[i][s[i][j] - 'a'];
    for (int j = 0; j < 26; ++j) if (cnts[0][j] != cnts[1][j]) return void(cout << "-1\n");
    vector<int> r[2];
    for (int i: {0, 1}) {
      r[i].resize(n);
      partial_sum(cnts[i], end(cnts[i]), cnts[i]), rotate(cnts[i], end(cnts[i]) - 1, end(cnts[i])), cnts[i][0] = 0;
      for (int j = 0; j < n; ++j) r[i][cnts[i][s[i][j] - 'a']++] = j;
    }
    for (auto x: r[0]) cout << x + 1 << ' '; cout << '\n';
    for (auto x: r[1]) cout << x + 1 << ' '; cout << '\n';
    return;
  }
  for (int x: {0, 1}) for (int y: {0, 1})
  for (int i = 0, p = 0; i < n * m; ++i) h[x][i + 1][y] = p = (p * 3ull + remap[s[x][i] - 'a'][y]) % mod;
  for (int d = 0; d < m; ++d) {
    to = {};
    byh = {};
    for (int i = 0; i < n; ++i) head[i] = hget(0, i * m, d), tail[i] = hget(0, i * m + d, m - d), byh[head[i]].push_back(i), nx[i] = -1;
    for (int i = 0; i < n; ++i) to[hget(1, i * m, m - d)].push_back(hget(1, i * m + m - d, d));
    byh[head[0]].erase(find(byh[head[0]].begin(), byh[head[0]].end(), 0));
    todo = {{}};
    if (!extend(0)) todo = {};
    else if (int x = todo.back(); to[tail[x]].empty() || to[tail[x]][0] != head[0]) todo = {};
    else to[tail[x]] = {}, nx[x] = 0;
    for (int i = 0; i < todo.size(); ++i) {
      int x = todo[i], y = nx[x];
      to[tail[x]].push_back(head[y]);
      if (!extend(x)) { todo = {}; break; }
      if (nx[x] == y) {
        to[tail[x]].pop_back();
        continue;
      }
      --i;
      x = todo.back();
      if (to[tail[x]].empty() || to[tail[x]][0] != head[y]) { todo = {}; break; }
      to[tail[x]] = {}, nx[x] = y;
    }
    if (todo.size() != n) continue;
    byh = {};
    for (int i = 0; i < n; ++i) byh[hget(1, i * m, m - d) * 2 ^ hget(1, i * m + m - d, d)].push_back(i);
    todo = q = {};
    for (int i = 0; todo.push_back(i), i = nx[i]; ) ;
    for (int i = 0; i < n; ++i) {
      auto h = tail[todo[i]] * 2 ^ head[todo[i + (i != n - 1 ?: 1 - n)]];
      q.push_back(byh[h].back()), byh[h].pop_back();
    }
    for (auto x: todo) cout << x + 1 << ' '; cout << '\n';
    for (auto x: q) cout << x + 1 << ' '; cout << '\n';
    return;
  }
  cout << "-1\n";
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  generate(*remap, *end(remap), mt19937_64{163467});
  for (int i = 0, p = 1; i < N; ++i) pw[i] = p, p = p * 3ull % mod;
  for (int tc = (cin >> tc, tc); tc--; ) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

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

output:


result: