This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define fi first
#define se second
#define pb push_back
#define endl "\n"
//~ #define int long long
using namespace std;
typedef tuple<int, int, int> iii;
typedef long long ll;
const int mod = 1e9+7;
//~ const int mod =998244353;
int n, k;
string s[55][1505];
int a[55][1505][5], ans[55][1505], pw[1505];
int32_t main(){
fast;
int n, k;
cin>>n>>k;
pw[0]= 1;
for(int i=1;i<=n;i++){
pw[i]=pw[i-1]*29%mod;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
cin>>s[i][j];
for(int l=0;l<i;l++){
a[i][j][0]+=(s[i][j][l]-'a'+1)*pw[l]%mod;
a[i][j][0]%=mod;
}
for(int l=0;l<i-1;l++){
a[i][j][1]+=(s[i][j][l]-'a'+1)*pw[l]%mod;
a[i][j][1]%=mod;
}
for(int l=1;l<i;l++){
a[i][j][2]+=(s[i][j][l]-'a'+1)*pw[l-1]%mod;
a[i][j][2]%=mod;
}
}
}
for(int i=1;i<=k;i++)ans[1][i]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<=k;j++){
for(int l=1;l<=k;l++){
if(a[i][j][1]==a[i-1][l][0]){
ans[i][j]+=ans[i-1][l];
ans[i][j]%=mod;
//~ cout<<" :: "<<l<<" k ";
}
else if(a[i][j][2]==a[i-1][l][0]){
ans[i][j]+=ans[i-1][l];
ans[i][j]%=mod;
//~ cout<<" :: "<<l<<" k2 ";
}
}
//~ cout<<" ^ "<<ans[i][j]<<" ^ ";
}
//~ cout<<endl;
}
//~ cout<<endl;
int ANS=0;
for(int i=1;i<=k;i++){
ANS+=ans[n][i];
ANS%=mod;
}
cout<<ANS<<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... |