Submission #498039

#TimeUsernameProblemLanguageResultExecution timeMemory
498039andrei_boacaCubeword (CEOI19_cubeword)C++17
100 / 100
329 ms22492 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int mod=998244353,alphsize=26,lgalph=62;
set<string> setik;
vector<int> muchii[25];
int nr[15][105][105];
int val[11];
int fact[5];
int n,ans,coef[15][500005];
bool found[11];
int toint(char c)
{
    if(islower(c))
        return c-'a'+1;
    if(isupper(c))
        return alphsize+c-'A'+1;
    if(isdigit(c))
        return 2*alphsize+c-'0'+1;
}
int gethash(int a, int b, int c)
{
    return a*lgalph*lgalph+b*lgalph+c;
}
int v[5];
int repperm(int a, int b,int c, int d)
{
    v[1]=a;
    v[2]=b;
    v[3]=c;
    v[4]=d;
    int rez=1*2*3*4;
    int lg=0;
    for(int i=1;i<=4;i++)
    {
        if(v[i]==v[i-1])
            lg++;
        else
        {
            rez=rez/fact[lg];
            lg=1;
        }
        if(i==4)
            rez=rez/fact[lg];
    }
    return rez;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    fact[0]=fact[1]=1;
    fact[2]=2;
    fact[3]=6;
    fact[4]=24;
    for(int i=1;i<=n;i++)
    {
        string a;
        cin>>a;
        setik.insert(a);
        reverse(a.begin(),a.end());
        setik.insert(a);
    }
    for(string s:setik)
    {
        int lg=s.size();
        int x=toint(s[0]);
        int y=toint(s[lg-1]);
        nr[lg][x][y]++;
        found[lg]=1;
    }
    for(int sz=3;sz<=10;sz++)
    {
        if(!found[sz])
            continue;
        found[sz]=0;
        for(int t=1;t<=lgalph;t++)
        {
            for(int x=1;x<=lgalph;x++)
            {
                if(nr[sz][x][t]==0)
                    continue;
                for(int y=x;y<=lgalph;y++)
                {
                    if(nr[sz][y][t]==0)
                        continue;
                    for(int z=y;z<=lgalph;z++)
                    {
                        if(nr[sz][z][t]==0)
                            continue;
                        int h=gethash(x,y,z);
                        int sol=(1LL*nr[sz][x][t]*nr[sz][y][t])%mod;
                        sol=(1LL*sol*nr[sz][z][t])%mod;
                        coef[sz][h]=(coef[sz][h]+sol)%mod;
                        if(coef[sz][h]!=0)
                            found[sz]=1;
                    }
                }
            }
        }
    }
    for(int sz=3;sz<=10;sz++)
    {
        if(!found[sz])
            continue;
        for(val[1]=1;val[1]<=lgalph;val[1]++)
            for(val[3]=val[1];val[3]<=lgalph;val[3]++)
                for(val[6]=val[3];val[6]<=lgalph;val[6]++)
                {
                    int h=gethash(val[1],val[3],val[6]);
                    if(coef[sz][h]==0)
                        continue;
                    for(val[8]=val[6];val[8]<=lgalph;val[8]++)
                    {
                        int sol=1;
                        int x=gethash(val[1],val[3],val[6]);
                        sol=(1LL*sol*coef[sz][x])%mod;
                        x=gethash(val[1],val[3],val[8]);
                        sol=(1LL*sol*coef[sz][x])%mod;
                        x=gethash(val[1],val[6],val[8]);
                        sol=(1LL*sol*coef[sz][x])%mod;
                        x=gethash(val[3],val[6],val[8]);
                        sol=(1LL*sol*coef[sz][x])%mod;
                        sol=(1LL*sol*repperm(val[1],val[3],val[6],val[8]))%mod;
                        ans=(ans+sol)%mod;
                    }
                }
    }
    cout<<ans;
    return 0;
}

Compilation message (stderr)

cubeword.cpp: In function 'int toint(char)':
cubeword.cpp:20:1: warning: control reaches end of non-void function [-Wreturn-type]
   20 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...