Submission #1343090

#TimeUsernameProblemLanguageResultExecution timeMemory
1343090nghiaxtoneriCubeword (CEOI19_cubeword)C++20
50 / 100
126 ms20636 KiB
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define MASK(n) (1LL << n)
#define PhTrNghia "cubeword"

using namespace std;

const int maxn = 1e5 + 5;
const int MOD = 998244353;

int id(char c){
    return c - 'a';
}

set <string> w[maxn];

int add(int a, int b){
    a += b;
    if (a >= MOD) a -= MOD;
    if (a < 0) a += MOD;
    return a;
}

int mul(int a, int b){
    return (a * b) % MOD;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //if (fopen(PhTrNghia".INP", "r")){
        //freopen(PhTrNghia".INP", "r", stdin);
        //freopen(PhTrNghia".OUT", "w", stdout);
    //}

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        string s;
        cin >> s;
        w[s.length()].insert(s);
        reverse(s.begin(), s.end());
        w[s.length()].insert(s);
    }

    int total = 0, K = 26;

    for (int len = 3; len <= 10; len++){
        if (w[len].empty()) continue;

        int a[26][26] = {};
        int dp[26][26][26] = {};

        for (string s : w[len]) a[id(s.front())][id(s.back())]++;

        for (int i = 0; i < K; i++)
            for (int j = 0; j < K; j++)
                for (int k = 0; k < K; k++)
                    for (int y = 0; y < K; y++)
                        dp[i][j][k] = add(dp[i][j][k], mul(mul(a[i][y], a[j][y]), a[k][y]));


        for (int x1 = 0; x1 < K; x1++){
            for (int x2 = 0; x2 < K; x2++){
                for (int x3 = 0; x3 < K; x3++){
                    for (int x4 = 0; x4 < K; x4++){
                        int res = mul(dp[x1][x2][x3], dp[x1][x2][x4]);
                        res = mul(res, dp[x1][x3][x4]);
                        res = mul(res, dp[x2][x3][x4]);
                        total = add(total, res);
                    }
                }
            }
        }
    }

    cout << total;
    return 0;
}
/*
1
radar

1
robot

2
baobab
bob
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...