QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#220432#5166. 回文匹配hos_lyric#0 0ms0kbC++145.4kb2023-10-20 11:55:572024-07-04 02:20:47

Judging History

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

  • [2024-07-04 02:20:47]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-10-20 11:55:57]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// |as| = n ==> |rs| = 2 n + 1
// [i - rs[i], i + rs[i]] is palindrome for $ as[0] $ as[1] $ ... $ as[n-1] $
// as[i, j): palindrome <=> j - i <= rs[i + j]
template <class String> vector<int> manacher(const String &as) {
  const int n = as.size();
  vector<int> rs(2 * n + 1);
  for (int i = 0, j = 0, k; i <= 2 * n; i += k, j -= k) {
    for (; 0 < i - j && i + j < 2 * n &&
           (!((i + j + 1) & 1) || as[(i - j - 1) >> 1] == as[(i + j + 1) >> 1]);
         ++j) {}
    rs[i] = j;
    for (k = 1; k < j && k + rs[i - k] < j; ++k) rs[i + k] = rs[i - k];
  }
  return rs;
}


char buf[500'010];

int T, N, Q;
vector<string> S;
vector<int> I, J;


namespace brute {
vector<int> run() {
cerr<<"[brute::run]"<<endl;
  vector<vector<int>> rss(N + 1);
  for (int i = 1; i <= N; ++i) {
    rss[i] = manacher(S[i]);
// cerr<<S[i]<<": "<<rss[i]<<endl;
  }
  vector<vector<int>> f(N + 1, vector<int>(N + 1, 0));
  for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) {
    const int lenI = S[i].size();
    const int lenJ = S[j].size();
    for (int l = 0; l <= lenJ - lenI; ++l) {
      bool ok = true;
      for (int x = 0; x <= 2 * lenI; ++x) {
        ok = ok && (rss[i][x] == min({rss[j][2 * l + x], x, 2 * lenI - x}));
      }
      if (ok) {
        ++f[i][j];
      }
    }
  }
  vector<int> ans(Q, 0);
  for (int q = 0; q < Q; ++q) {
    ans[q] = f[I[q]][J[q]];
  }
  return ans;
}
}  // brute


namespace sub2 {
vector<int> run() {
cerr<<"[sub2::run]"<<endl;
  vector<pair<vector<int>, int>> ps(N + 1);
  for (int i = 1; i <= N; ++i) {
    ps[i] = make_pair(manacher(S[i]), i);
  }
  sort(ps.begin() + 1, ps.end());
  int id = 0;
  vector<int> ids(N + 1, -1);
  for (int i = 1, j = 1; i <= N; i = j) {
    for (; j <= N && ps[i].first == ps[j].first; ++j) {
      ids[ps[j].second] = id;
    }
    ++id;
  }
  vector<int> ans(Q, 0);
  for (int q = 0; q < Q; ++q) {
    ans[q] = (ids[I[q]] == ids[J[q]]) ? 1 : 0;
  }
  return ans;
}
}  // sub2


namespace slow {
vector<int> L;
vector<vector<int>> R, leftLens, fail;
map<pair<int, int>, int> cache;
int solve(int u, int v) {
  auto it = cache.find(make_pair(u, v));
  if (it != cache.end()) return it->second;
  int ret = 0;
  if (L[u] <= L[v]) {
    int j = 0, c = 0;
    for (int i = 1; i <= L[v]; ++i) {
      for (; ; j = fail[u][j]) {
        chmax(c, 2 * i - j);
        for (; c + R[v][c - 1] < 2 * i; ++c) {}
        if (leftLens[u][j + 1] == 2 * i - c + 1) break;
      }
      if (++j == L[u]) {
        ++ret;
        j = fail[u][j];
      }
    }
  }
  return cache[make_pair(u, v)] = ret;
}
vector<int> run() {
cerr<<"[slow::run]"<<endl;
  L.assign(N + 1, 0);
  R.assign(N + 1, {});
  leftLens.assign(N + 1, {});
  fail.assign(N + 1, {});
  for (int u = 1; u <= N; ++u) {
    L[u] = S[u].size();
    R[u] = manacher(S[u]);
    leftLens[u].assign(2 * L[u] + 1, 0);
    for (int i = 0; i <= 2 * L[u]; ++i) chmax(leftLens[u][(i + R[u][i]) / 2], R[u][i]);
    for (int i = L[u]; --i >= 0; ) chmax(leftLens[u][i], leftLens[u][i + 1] - 2);
    fail[u].assign(L[u] + 1, 0);
    {
      int j = 0, c = 0;
      for (int i = 2; i <= L[u]; ++i) {
        for (; ; j = fail[u][j]) {
          chmax(c, 2 * i - j);
          for (; c + R[u][c - 1] < 2 * i; ++c) {}
          if (leftLens[u][j + 1] == 2 * i - c + 1) break;
        }
        fail[u][i] = ++j;
      }
    }
  }
  vector<int> ans(Q);
  for (int q = 0; q < Q; ++q) {
    ans[q] = solve(I[q], J[q]);
  }
  return ans;
}
}  // slow


int main() {
  for (; ~scanf("%d%d%d", &T, &N, &Q); ) {
    assert(T == 0);
    S.resize(N + 1);
    for (int i = 1; i <= N; ++i) {
      scanf("%s", buf);
      S[i] = buf;
    }
    S[0] = "";
    I.resize(Q);
    J.resize(Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%d", &I[q], &J[q]);
    }
    
    bool spe2 = true;
    for (int i = 1; i <= N; ++i) {
      spe2 = spe2 && (S[1].size() == S[i].size());
    }
    
    vector<int> ans;
    if (spe2) {
      ans = sub2::run();
    } else {
      ans = slow::run();
    }
    for (int q = 0; q < Q; ++q) {
      printf("%d\n", ans[q]);
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Judgement Failed

input:


output:


result:


Test #2:

score: 0
Judgement Failed

input:


output:


result:


Test #3:

score: 0
Judgement Failed

input:


output:


result:


Test #4:

score: 0
Judgement Failed

input:


output:


result:


Test #5:

score: 0
Judgement Failed

input:


output:


result:


Test #6:

score: 0
Judgement Failed

input:


output:


result:


Test #7:

score: 0
Judgement Failed

input:


output:


result:


Test #8:

score: 0
Judgement Failed

input:


output:


result:


Test #9:

score: 0
Judgement Failed

input:


output:


result:


Test #10:

score: 0
Judgement Failed

input:


output:


result: