This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
const int MOD = (int) 1e9 + 7;
const int MOD2 = (int) 1e9 + 9;
int P = 29;
int add(int a,int b){
if(a+b>=MOD) return a+b-MOD;
return a+b;
}
int mult(int a,int b){
if(a*b>=MOD) return a*b%MOD;
return a*b;
}
array<int,3> get_hash(string s){
int res=0LL;
for(int i=0;i<sz(s)-1;i++)
{
res=mult(res,P);
res=add(res,s[i]-'a'+1);
}
int res2=0LL;
for(int i=1;i<sz(s);i++)
{
res2=mult(res2,P);
res2=add(res2,s[i]-'a'+1);
}
int res3=0LL;
for(int i=0;i<sz(s);i++)
{
res3=mult(res3,P);
res3=add(res3,s[i]-'a'+1);
}
return {res,res2,res3};
}
void solve(){
int n,k;
cin >> n >> k;
vector<array<int,3>> hash[n+1];
vector<string> v[n+1];
map<string,int> mp;
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
string s; cin >> s;
mp[s]++;
if(mp[s]==1){
v[i].pb(s);
hash[i].pb(get_hash(s));
}
}
}
vector<int> dp[n+1];
vector<int> freq[n+1];
for(int i=1;i<=n;i++){
for(string s:v[i]){
freq[i].pb(mp[s]);
}
}
for(int i=0;i<sz(v[1]);i++) dp[1].pb(freq[1][i]);
for(int i=2;i<=n;i++){
for(int j=0;j<sz(v[i]);j++){
int ans=0;
for(int z=0;z<sz(v[i-1]);z++){
if(hash[i][j][0]==hash[i-1][z][2] || hash[i][j][1]==hash[i-1][z][2])
ans=add(ans,dp[i-1][z]);
}
dp[i].pb(mult(freq[i][j],ans));
}
}
int ans=0;
for(int x:dp[n]) ans=add(ans,x);
cout << ans << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |