/**
* author: kututay
* created: 06.09.2023 16:41:38
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/Users/kutay/CP/templates/debug.h"
#else
#define debug(...) void(38)
#endif
#define int long long
const int mod = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k; cin >> n >> k;
vector<vector<int>> a(n * k + 2);
vector<int> dp(n * k + 2, -1), cnt(n * k + 2);
function<int(int)> f = [&](int node) {
if (dp[node] != -1) return dp[node];
int res = 0;
sort(a[node].begin(), a[node].end());
for (int i = 0; i < (int) a[node].size(); i++) {
int next = a[node][i];
if (i > 0 && a[node][i - 1] == next) continue;
res += (cnt[node] * f(next)) % mod;
res %= mod;
}
return dp[node] = res;
};
map<string, int> m;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
string s; cin >> s;
if (m.count(s) == 0) {
m[s] = ++count;
}
int node = m[s];
cnt[node]++;
if (i == n - 1) dp[node] = cnt[node];
if (i > 0) {
if (m.count(s.substr(0, i))) a[m[s.substr(0, i)]].emplace_back(node);
if (m.count(s.substr(1, i))) a[m[s.substr(1, i)]].emplace_back(node);
} else {
if (cnt[node] == 1) {
a[n * k + 1].emplace_back(node);
}
}
}
}
cnt[n * k + 1] = 1;
cout << f(n * k + 1) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1112 KB |
Output is correct |
2 |
Correct |
8 ms |
1288 KB |
Output is correct |
3 |
Correct |
7 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
6 ms |
1116 KB |
Output is correct |
6 |
Correct |
6 ms |
1116 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
1112 KB |
Output is correct |
6 |
Correct |
8 ms |
1288 KB |
Output is correct |
7 |
Correct |
7 ms |
1116 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
6 ms |
1116 KB |
Output is correct |
10 |
Correct |
6 ms |
1116 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
143 ms |
13216 KB |
Output is correct |
13 |
Correct |
137 ms |
13100 KB |
Output is correct |
14 |
Correct |
135 ms |
12864 KB |
Output is correct |
15 |
Correct |
133 ms |
12968 KB |
Output is correct |
16 |
Correct |
30 ms |
4712 KB |
Output is correct |
17 |
Correct |
135 ms |
10840 KB |
Output is correct |
18 |
Correct |
132 ms |
10960 KB |
Output is correct |
19 |
Correct |
126 ms |
10832 KB |
Output is correct |
20 |
Correct |
130 ms |
10996 KB |
Output is correct |
21 |
Correct |
142 ms |
11084 KB |
Output is correct |
22 |
Correct |
34 ms |
4700 KB |
Output is correct |