QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#845405#9970. Looping RPSucup-team6225WA 6ms58272kbC++173.1kb2025-01-06 16:33:392025-01-06 16:33:39

Judging History

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

  • [2025-01-06 16:33:39]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:58272kb
  • [2025-01-06 16:33:39]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;

using ll = long long;   using pii = pair<int, int>;
const int N = 100010, P = 13331, M = 2000010, SZ = (1 << 18) + 5;
static char buf[SZ], *bgn = buf, *til = buf;
char getc() {
    if(bgn == til)  bgn = buf, til = buf + fread(buf, 1, SZ, stdin);
    return bgn == til ? EOF : *bgn++;
}
template<typename T>
void read(T &x) {
    char ch = getc();  T fh = 0;   x = 0;
    while(ch < '0' || ch > '9')    fh |= (ch == '-'), ch = getc();
    while(ch >= '0' && ch <= '9')   x = x * 10 + ch - '0', ch = getc();
    x = fh ? -x : x;
}
template<typename Type, typename... argc>
void read(Type &x, argc &...args)   {read(x), read(args...);}
int n, to[300];
string s[N];
map<string, int> has, nid;
ll ans;
template<const int Mod>
struct Hash {
    string str; int len;    int pw[N], hsh[N];
    const int mod = Mod;
    void init(string t) {
        str = t, len = str.length() - 1, pw[0] = 1;
        for(int i = 1; i <= len; ++i)   pw[i] = 1ll * pw[i - 1] * P % mod;
        for(int i = 1; i <= len; ++i)   hsh[i] = (1ll * hsh[i - 1] * P % mod + str[i]) % mod;
    } 
    int get(int l, int r)   {return (hsh[r] - 1ll * hsh[l - 1] * pw[r - l + 1] % mod + mod) % mod;}
};
Hash<1000000007> hx;
struct Trie {
    int ch[M][3], siz[M], idx = 1, root = 1;
    vector<pii> f[M];
    void insert(string str, int id, int num) {
        str = str + str;
        int u = root;
        for(int i = 0; i < str.length(); ++i) {
            int v = to[str[i]];
            if(!ch[u][v])   ch[u][v] = ++idx;
            u = ch[u][v], siz[u] += num;
        }
        f[u].pb({id, num});
    }
    void dfs(int x, int dep, int id, int num) {
        int sum[3] = {0}, nid[3] = {0};
        for(int i = 0; i < 3; ++i)  sum[i] = siz[ch[x][i]];
        for(int i = 0; i < 3; ++i) {
            if(id && to[s[id][dep % s[id].length()]] == i)  sum[i] += num, nid[i] = id;
            for(auto [j, y] : f[x]) if(to[s[j][dep % s[j].length()]] == i)   nid[i] = j, sum[i] += y;
        }
        ans += 1ll * sum[0] * sum[1] * sum[2];
        // cerr << x << " : " << sum[0] << " " << sum[1] << " " << sum[2] << "\n";
        // if(sum[0] + sum[1] + sum[2] < 3)    return;
        for(int i = 0; i < 3; ++i)  if(ch[x][i])    dfs(ch[x][i], dep + 1, nid[i], sum[i] - siz[i]);
    }
}tree;
int main() {
    #ifdef Kelly
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        freopen("err.txt", "w", stderr);
    #endif
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n;
    to['P'] = 0, to['K'] = 1, to['N'] = 2;
    for(int i = 1; i <= n; ++i) cin >> s[i];
    for(int i = 1; i <= n; ++i) {
        int len = s[i].length();
        hx.init(" " + s[i]);    string nw = s[i];
        for(int j = 1; j <= len; ++j)   if(len % j == 0 && hx.get(1, j) == hx.get(len - j + 1, len)) {
            nw = s[i].substr(0, j); break;
        }
        s[i] = nw, ++has[nw], nid[nw] = i;
    }
    for(auto [str, x] : has)    tree.insert(str, nid[str], x);
    tree.dfs(tree.root, 0, 0, 0);
    cout << ans;
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 58272kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 57828kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

1

result:

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