QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#843717 | #9970. Looping RPS | ucup-team1198# | WA | 2ms | 14116kb | C++20 | 3.3kb | 2025-01-04 23:03:52 | 2025-01-04 23:03:52 |
Judging History
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_pi(string s) {
int n = (int)s.size();
vector<int> pi(n);
for (int i = 1; i < n; ++i) {
int j = pi[i - 1];
while (j != -1 && s[j] != s[i])
j = (j == 0 ? -1 : pi[j - 1]);
pi[i] = j + 1;
}
return pi;
}
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) {
if (real_go[v][i] == -1) {
szs[i] = 0;
periods[i] = depth[v] + 1;
continue;
}
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]);
}
}
int len = depth[go[v][i]];
int pref_fun = depth[suff[go[v][i]]];
periods[i] = len - pref_fun;
szs[i] = term[go[v][i]];
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] * 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();
int pref = get_pi(s).back();
if (len % (len - pref) == 0)
s.resize(len - pref);
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;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 13868kb
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: 14116kb
input:
10 KKKNP KNKPPKNK KNKPP KNKPPKN KKKN NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN NNKN NPPN NNKNNNKNNNKNNNKNNNKNNNKNNNK KKKNN
output:
2
result:
wrong answer 1st numbers differ - expected: '3', found: '2'