QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#843722#9970. Looping RPSucup-team1198#WA 2ms13888kbC++203.2kb2025-01-04 23:15:042025-01-04 23:15:12

Judging History

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

  • [2025-01-04 23:15:12]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:13888kb
  • [2025-01-04 23:15:04]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

vector<int> get_z(string s) {
  int n = (int)s.size();
  int j = 0;
  vector<int> z(n);
  for (int i = 1; i < n; ++i) {
    if (j + z[j] > i)
      z[i] = min(z[i - j], j + z[j] - i);
    while (s[z[i]] == s[i + z[i]])
      ++z[i];
    if (i + z[i] > j + z[j])
      j = i;
  }
  return z;
}

const int MAXN = 3'000'100;
const int ALPHA = 3;

int go[MAXN][ALPHA];
int suff[MAXN];

int depth[MAXN];
int real_go[MAXN][ALPHA];
int non3term[MAXN];
int term[MAXN];
int nodes_cnt = 0;

int node() {
  fill(real_go[nodes_cnt], real_go[nodes_cnt] + ALPHA, -1);
  depth[nodes_cnt] = 0;
  term[nodes_cnt] = 0;
  ++nodes_cnt;
  return nodes_cnt - 1;
}

void dfs_cnt(int v) {
  for (int i = 0; i < ALPHA; ++i) {
    if (real_go[v][i] == -1)
      continue;
    dfs_cnt(real_go[v][i]);
    term[v] += term[real_go[v][i]];
  }
}

long long ans = 0;

vector<int> stack;

void dfs_karasik(int v) {
  stack.emplace_back(v);
  int szs[ALPHA];
  int periods[ALPHA];
  
  fill(go[v], go[v] + ALPHA, -1);
  
  for (int i = 0; i < ALPHA; ++i) {
    periods[i] = depth[v] + 1 - depth[v == 0 ? 0 : go[suff[v]][i]];  
    if (real_go[v][i] == -1) {
      szs[i] = 0;
      continue;
    }
    szs[i] = term[real_go[v][i]];

    go[v][i] = real_go[v][i];
    for (int j = 0; j < ALPHA; ++j) {
      if (go[v][j] == -1) {
        go[v][j] = (v == 0 ? 0 : go[suff[v]][j]);
      } else {
        suff[go[v][j]] = (v == 0 ? 0 : go[suff[v]][j]);
      }
    }
    dfs_karasik(go[v][i]);
    go[v][i] = -1;
  }
  ans += szs[0] * 1ll * szs[1] * 1ll * szs[2];
  for (int i = 0; i < ALPHA; ++i) {
    if (periods[i] * 3 <= depth[v])
      ans += non3term[stack[periods[i]]] * 1ll * szs[(i + 1) % 3] * 1ll * szs[(i + 2) % 3];
  }

  stack.pop_back();
}



int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  node();
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;

    int len = s.size();
    auto z = get_z(s);
    int per = 1;
    while (per < len && z[per] != len - per)
      ++per;
    s.resize(per);

    vector<int> cur;
    for (char c : s) {
      if (c == 'P')
        cur.emplace_back(0);
      else if (c == 'N')
        cur.emplace_back(1);
      else
        cur.emplace_back(2);
    }
    int cur_node = 0;
    for (int x : cur) {
      if (real_go[cur_node][x] == -1) {
        real_go[cur_node][x] = node();
        depth[real_go[cur_node][x]] = depth[cur_node] + 1;
      }
      cur_node = real_go[cur_node][x];
    }
    ++non3term[cur_node];
    for (int j = 0; j < 2; ++j) {
      for (int x : cur) {
        if (real_go[cur_node][x] == -1) {
          real_go[cur_node][x] = node();
          depth[real_go[cur_node][x]] = depth[cur_node] + 1;
        }
        cur_node = real_go[cur_node][x];
      }
    }
    ++term[cur_node];
  }
  dfs_cnt(0);
  dfs_karasik(0);
  cout << ans << '\n';
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13756kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 13888kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

1

result:

wrong answer 1st numbers differ - expected: '3', found: '1'