QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#843893 | #9970. Looping RPS | ucup-team3608# | WA | 1ms | 3816kb | C++23 | 2.5kb | 2025-01-05 06:35:50 | 2025-01-05 06:35:50 |
Judging History
answer
#include <bits/stdc++.h>
#define st first
#define nd second
using lint = long long;
constexpr int mod = 998244353;
constexpr int inf = 0x3f3f3f3f;
constexpr lint linf = 0x3f3f3f3f3f3f3f3f;
using namespace std;
vector<int> prefix_function(const string& S) {
vector<int> fail = {-1}; fail.reserve(S.size());
for (int i = 0; i < int(S.size()); ++i) {
int j = fail.back();
while (j != -1 && S[i] != S[j]) j = fail[j];
fail.push_back(j+1);
}
return fail;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin>>n;
vector<string>s(n);
vector<vector<int>>kmp(n);
for(int i=0;i<n;i++) cin>>s[i], kmp[i] = prefix_function(s[i]);
array<int, 3>iden = {-1, -1, -1};
vector<array<int, 3>>trie(1, iden);
vector<int>f(1), h(1), id(1, -1), period(1);
vector<vector<int>>v(1);
for(int i=0;i<n;i++){
int cur = 0;
vector<int>states(1);
int sz = (int)s[i].size();
for(int j=0;j<sz;j++){
char ch = s[i][j];
int c = 0;
if(ch == 'K') c = 0;
if(ch == 'P') c = 1;
if(ch == 'N') c = 2;
if(trie[cur][c] == -1){
trie[cur][c] = (int)trie.size();
trie.push_back(iden);
v.push_back({});
f.push_back(0);
id.push_back(i);
period.push_back(0);
h.push_back(j + 1);
}
cur = trie[cur][c];
states.push_back(cur);
period[cur] = states[j + 1 - kmp[i][j + 1]];
f[cur]++;
}
int per = sz - kmp[i][sz];
if(sz % per) v[cur].push_back(h[cur]);
else v[period[cur]].push_back(h[period[cur]]);
}
int sz = (int)trie.size();
for(auto &vcur : v){
sort(vcur.begin(), vcur.end());
}
lint ans = 0;
for(int i=0;i<sz;i++){
auto [a, b, c] = trie[i];
lint x = (a == -1) ? 0 : f[a];
lint y = (b == -1) ? 0 : f[b];
lint z = (c == -1) ? 0 : f[c];
if(i){
int back = period[i];
int rest = h[i] % h[back];
int num = int(upper_bound(v[back].begin(), v[back].end(), h[i]) - v[back].begin());
char ch = s[id[back]][rest];
if(ch == 'K') x += num;
if(ch == 'P') y += num;
if(ch == 'N') z += num;
}
ans += x * y * z;
}
cout<<ans<<"\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3816kb
input:
6 P PN KK N PKK PN
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3624kb
input:
10 KKKNP KNKPPKNK KNKPP KNKPPKN KKKN NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN NNKN NPPN NNKNNNKNNNKNNNKNNNKNNNKNNNK KKKNN
output:
1
result:
wrong answer 1st numbers differ - expected: '3', found: '1'