#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int N=257,mod=998244353;
const char LCH='z';
vector<int> ma[N],pth,rma[N];
char val[N];
set<string> cnt1[20][300][300];
ll cnt[20][300][300];
ll dp[N][N];
bool gonxt() // fix only 4 value
{
val[1]='a';
val[4]++;
for(int i=4;i>1;i--)
{
if(val[i]>LCH)
{
val[i]='a';
val[i-1]++;
}
}
if(val[1]=='b')
{
return 0;
}
return 1;
}
ll count(int len)
{
// cout<<"AT ";
// for(int i=1;i<=8;i++)cout<<val[i]<<' ';
// cout<<endl;
ll ans=0;
for(int i=1;i<=1;i++)
{
for(char c='a';c<=LCH;c++)
{
ll cur=1;
for(auto j:ma[i])
{
cur*=cnt[len][c][val[j]];
cur=(cur%mod);
}
ans=(ans+cur)%mod;
}
}
// if(ans>0)
// {
// cout<<"AT ";
// for(int i=1;i<=8;i++)cout<<val[i]<<' ';
// cout<<endl;
// cout<<ans<<' ';
// cout<<endl;
// }
for(int i=5;i<=7;i++)
{
for(char c='a';c<=LCH;c++)
{
dp[i][c]=1;
for(auto j:rma[i])
{
dp[i][c]=(dp[i][c]*(ll)(cnt[len][c][val[j]]))%mod;
}
}
}
ll fnl=0;
for(int i=8;i<=8;i++)
{
for(char c='a';c<=LCH;c++)
{
dp[i][c]=1;
ll sm=0,sm1=0,sm2=0;
for(char c1='a';c1<=LCH;c1++)
{
sm=(sm+(dp[5][c1]*(ll)(cnt[len][c][c1]))%mod)%mod;
sm1=(sm1+(dp[6][c1]*(ll)(cnt[len][c][c1]))%mod)%mod;
sm2=(sm2+(dp[7][c1]*(ll)(cnt[len][c][c1]))%mod)%mod;
}
dp[i][c]=(dp[i][c]*sm)%mod;
dp[i][c]=(dp[i][c]*sm1)%mod;
dp[i][c]=(dp[i][c]*sm2)%mod;
fnl+=(dp[i][c]*ans)%mod;
fnl%=mod;
}
}
return fnl;
}
int32_t main()
{
// cout<<('p'-'a'+1)<<endl;
ma[1].push_back(2); // 1 2
ma[1].push_back(3);// 1 3
ma[1].push_back(4);// 1 5
ma[2].push_back(5);// 2 4
ma[2].push_back(6);// 2 6
ma[3].push_back(5); // 3 4
ma[3].push_back(7);// 3 7
ma[5].push_back(8);// 4 8
ma[4].push_back(6); // 5 6
ma[4].push_back(7); // 5 7
ma[6].push_back(8); // 6 8
ma[7].push_back(8); // 7 8
// dfs(1);
for(int i=1;i<=8;i++)
{
for(auto j:ma[i])
{
// cout<<i<<' '<<j<<endl;
rma[j].push_back(i);
}
}
int n;
cin>>n;
// cout<<('a'+0)<<endl;
// cout<<('A'+0)<<endl;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
// for(auto&x:s)
// {
// if(x>='a')
// {
// x='P'+(x-'a'+1);
// }
// }
int len=s.size();
string t=s;
// cout<<s<<endl;
reverse(begin(t),end(t));
cnt1[len][s[0]][s.back()].insert(s);
cnt1[len][s.back()][s[0]].insert(t);
}
for(int l=3;l<=10;l++)
{
for(char a='a';a<=LCH;a++)
{
for(char b='a';b<=LCH;b++)
{
cnt[l][a][b]=cnt1[l][a][b].size();
// if(a=='R' and b=='R')
// {
// cout<<l<<' '<<cnt1[l][a][b].size()<<endl;
// }
}
}
}
// cout<<(LCH-'A'+1)<<endl;
ll sm=0;
for(int x=1;x<=8;x++)val[x]='?';
for(int len=3;len<=10;len++)
{
for(int x=1;x<=4;x++)
{
val[x]='a';
}
do
{
sm+=count(len)%mod;
sm%=mod;
}while(gonxt());
}
cout<<sm<<endl;
}
| # | 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... |