#include<bits/stdc++.h>
using namespace std;
const int const1 = 1e9 + 7;
const int const2 = 998244353;
vector <vector <vector <pair <int, int> > > > g;
vector <vector <bool> > used;
vector <vector <int> > dist;
int n, k;
vector <vector <pair <pair <int, int>, pair <pair <int, int>, pair <int, int> > > > > hashes;
void dfs(int a, int b)
{
used[a][b] = 1;
if(a == 0)
{
dist[a][b] = 1;
return;
}
for(int i = 0; i < k; i++)
{
if(hashes[a - 1][i].first == hashes[a][b].second.first || hashes[a - 1][i].first == hashes[a][b].second.second)
{
if(!used[a - 1][i])
{
dfs(a - 1, i);
}
dist[a][b] += dist[a - 1][i];
if(dist[a][b] >= const1)
{
dist[a][b] -= const1;
}
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
used.resize(n, vector <bool> (k));
dist.resize(n, vector <int> (k));
hashes.resize(n);
vector <int> Pow(n + 1), Pow1(n + 1);
Pow[0] = 1;
Pow1[0] = 1;
for(int i = 1; i <= n; i++)
{
Pow1[i] = 1LL * Pow1[i - 1] * 27 % const2;
Pow[i] = 1LL* Pow[i - 1] * 27 % const1;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < k; j++)
{
string s;
cin >> s;
int hash1 = 0;
int hash2 = 0;
int hash3, hash4, hash5, hash6;
for(int p = 0; p < i + 1; p++)
{
hash1 = (1LL * hash1 * 27 + s[p] - 'a' + 1) % const1;
hash2 = (1LL * hash2 * 27 + s[p] - 'a' + 1) % const2;
}
hash3 = 0;
hash4 = 0;
for(int p = 1; p < i + 1; p++)
{
hash3 = (1LL * hash3 * 27 + s[p] - 'a' + 1) % const1;
hash4 = (1LL * hash4 * 27 + s[p] - 'a' + 1) % const2;
}
hash5 = 0;
hash6 = 0;
for(int p = 0; p < i; p++)
{
hash5 = (1LL * hash5 * 27 + s[p] - 'a' + 1) % const1;
hash6 = (1LL * hash6 * 27 + s[p] - 'a' + 1) % const2;
}
hashes[i].push_back({{hash1, hash2}, {{hash3, hash4}, {hash5, hash6}}});
}
sort(hashes[i].begin(), hashes[i].end());
}
int ans = 0;
for(int i = 0;i < k; i++)
{
dfs(n - 1, i);
ans += dist[n - 1][i];
if(ans >= const1)
{
ans -= const1;
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
7 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
7 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
7 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
512 KB |
Output is correct |
12 |
Correct |
86 ms |
2552 KB |
Output is correct |
13 |
Correct |
87 ms |
2680 KB |
Output is correct |
14 |
Correct |
89 ms |
2556 KB |
Output is correct |
15 |
Correct |
91 ms |
2552 KB |
Output is correct |
16 |
Correct |
723 ms |
4600 KB |
Output is correct |
17 |
Correct |
141 ms |
4672 KB |
Output is correct |
18 |
Correct |
147 ms |
4472 KB |
Output is correct |
19 |
Correct |
149 ms |
4632 KB |
Output is correct |
20 |
Correct |
154 ms |
4472 KB |
Output is correct |
21 |
Correct |
150 ms |
4600 KB |
Output is correct |
22 |
Correct |
698 ms |
4600 KB |
Output is correct |