#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1880 KB |
Output is correct |
2 |
Correct |
7 ms |
1372 KB |
Output is correct |
3 |
Correct |
7 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
1372 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
1880 KB |
Output is correct |
6 |
Correct |
7 ms |
1372 KB |
Output is correct |
7 |
Correct |
7 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
1372 KB |
Output is correct |
10 |
Correct |
6 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
196 ms |
16936 KB |
Output is correct |
13 |
Correct |
222 ms |
16720 KB |
Output is correct |
14 |
Correct |
202 ms |
17396 KB |
Output is correct |
15 |
Correct |
197 ms |
17032 KB |
Output is correct |
16 |
Correct |
11 ms |
348 KB |
Output is correct |
17 |
Correct |
163 ms |
12800 KB |
Output is correct |
18 |
Correct |
142 ms |
12884 KB |
Output is correct |
19 |
Correct |
135 ms |
12628 KB |
Output is correct |
20 |
Correct |
133 ms |
12640 KB |
Output is correct |
21 |
Correct |
132 ms |
12800 KB |
Output is correct |
22 |
Correct |
13 ms |
348 KB |
Output is correct |