Submission #1281357

#TimeUsernameProblemLanguageResultExecution timeMemory
1281357icebearCubeword (CEOI19_cubeword)C++20
50 / 100
325 ms18372 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template<class T>
    bool minimize(T &a, const T &b) {
        if (a > b) return a = b, true;
        return false;
    }

template<class T>
    bool maximize(T &a, const T &b) {
        if (a < b) return a = b, true;
        return false;
    }

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
/*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */

const int MOD = 998244353;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int n;
int cnt[15][256][256], dp[26][26][26][26], f[26][26][26][26];
set<string> S;

void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
}

void init(void) {
    cin >> n;
    FOR(i, 1, n) {
        string s; cin >> s;
        if (S.find(s) != S.end()) continue;
        cnt[(int)s.size()][s[0] - 'a'][s.back() - 'a']++;
        S.insert(s);
        reverse(all(s));
        if (S.find(s) == S.end()) {
            S.insert(s);
            cnt[(int)s.size()][s[0] - 'a'][s.back() - 'a']++;
        }
    }
}

void process(void) {
    int ans = 0;
    FOR(l, 3, 10) {
        REP(a, 20) REP(b, 20) REP(c, 20) REP(d, 20)
            dp[a][b][c][d] = 1LL * cnt[l][a][b] * cnt[l][b][c] % MOD * cnt[l][c][d] % MOD * cnt[l][d][a] % MOD;

        FOR(turn, 5, 8) {
            REP(a, 20) REP(b, 20) REP(c, 20) REP(d, 20) REP(e, 20)
                add(f[b][c][d][e], 1LL * dp[a][b][c][d] * (turn == 5 ? 1 : cnt[l][d][e]) * cnt[l][a][e] % MOD);
            REP(a, 20) REP(b, 20) REP(c, 20) REP(d, 20) {
                dp[a][b][c][d] = f[a][b][c][d];
                f[a][b][c][d] = 0;
            }
        }

        REP(a, 20) REP(b, 20) REP(c, 20) REP(d, 20)
            add(ans, 1LL * dp[a][b][c][d] * cnt[l][d][a] % MOD);
    }
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//    cin >> tc;
    while(tc--) {
        init();
        process();
    }
    return 0;
}

Compilation message (stderr)

cubeword.cpp: In function 'int main()':
cubeword.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cubeword.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...