// File cubeword.cpp created on 08.10.2025 at 10:41:52 08
#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
template<typename T>
T power(T a, i64 b) {
T res {1};
while (b) {
if (b & 1) {
res *= a;
}
a *= a;
b >>= 1;
}
return res;
}
constexpr int md = 998244353;
struct MInt {
int val;
MInt() : val(0) {}
template<typename T>
MInt(T x) {
if (-md <= x && x < md) {
val = x;
} else {
val = x % md;
}
if (val < 0) {
val += md;
}
}
int operator()() { return val; }
MInt& operator+= (MInt rhs) {
if ((val += rhs.val) >= md) {
val -= md;
}
return *this;
}
MInt& operator-= (MInt rhs) {
if ((val -= rhs.val) < 0) {
val += md;
}
return *this;
}
MInt& operator*= (MInt rhs) {
val = int(1LL * val * rhs.val % md);
return *this;
}
MInt inv() {
return power(*this, md - 2);
}
MInt& operator/= (MInt rhs) {
return *this *= rhs.inv();
}
bool operator== (MInt rhs) {
return val == rhs.val;
}
bool operator!= (MInt rhs) {
return val != rhs.val;
}
};
MInt operator+ (MInt lhs, MInt rhs) {
return lhs += rhs;
}
MInt operator- (MInt lhs, MInt rhs) {
return lhs -= rhs;
}
MInt operator* (MInt lhs, MInt rhs) {
return lhs *= rhs;
}
MInt operator/ (MInt lhs, MInt rhs) {
return lhs /= rhs;
}
using Z = MInt;
constexpr int SZ = 32;
i64 hash(std::vector<int>& vec) {
i64 x = 0;
for (auto i : vec) {
x = x * (SZ + 1) + (i + 1);
}
return x;
}
Z solve(int n, std::vector<std::vector<int>> vecs) {
debug(1);
Z cnt[SZ][SZ] {};
std::set<i64> vis;
for (auto v : vecs) {
for (int _ = 0; _ < 2; ++_) {
i64 h = hash(v);
if (!vis.count(h)) {
vis.emplace(h);
cnt[v[0]][v[n - 1]] += 1;
}
std::reverse(v.begin(), v.end());
}
}
Z f[SZ][SZ][SZ];
for (int a = 0; a < SZ; ++a) {
for (int b = 0; b < SZ; ++b) {
for (int c = 0; c < SZ; ++c) {
for (int d = 0; d < SZ; ++d) {
f[b][c][d] += cnt[a][b] * cnt[a][c] * cnt[a][d];
}
}
}
}
Z ans = 0;
for (int a = 0; a < SZ; ++a) {
for (int b = 0; b < SZ; ++b) {
for (int c = 0; c < SZ; ++c) {
for (int d = 0; d < SZ; ++d) {
ans += f[a][b][c] * f[a][b][d] * f[a][c][d] * f[b][c][d];
}
}
}
}
return ans;
}
int alp[256];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int siz = 0;
for (char c = 'a'; c <= 'z'; ++c) {
alp[c] = siz++;
}
for (char c = 'A'; c <= 'Z'; ++c) {
alp[c] = siz++;
}
for (char c = '0'; c <= '9'; ++c) {
alp[c] = siz++;
}
int N;
std::cin >> N;
std::vector<std::vector<int>> A(N);
for (int i = 0; i < N; ++i) {
std::string S;
std::cin >> S;
int n = int(S.size());
A[i].resize(n);
for (int j = 0; j < n; ++j) {
A[i][j] = alp[S[j]];
}
}
std::vector<std::vector<std::vector<int>>> groups(11);
for (int i = 0; i < N; ++i) {
groups[A[i].size()].push_back(A[i]);
}
Z ans = 0;
for (int i = 3; i <= 10; ++i) {
debug(i, groups[i]);
ans += solve(i, groups[i]);
}
std::cout << ans.val << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |