/*
COCI 2020 Trener
- Notice that we only need to check consecutive words and that only the first and last
letters can differ
- We just use a map and some dp to solve this
- Complexity: O(NK^2)
*/
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
const ll M = 1e9 + 7;
ll dp[1501];
map<string, ll> mp;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
FOR(i, 1, n + 1) FOR(j, 1, k + 1) {
string s;
cin >> s;
dp[j] = (i > 1 ? mp[s.substr(0, i - 1)] + mp[s.substr(1, i - 1)] : 1);
if (i > 1 && s.substr(0, i - 1) == s.substr(1, i - 1)) dp[j] -= mp[s.substr(1, i - 1)];
if (dp[j] >= M) dp[j] -= M;
mp[s] += dp[j];
if (mp[s] >= M) mp[s] -= M;
}
ll ans = 0;
FOR(i, 1, k + 1) {
ans += dp[i];
if (ans >= M) ans -= M;
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1536 KB |
Output is correct |
2 |
Correct |
15 ms |
1536 KB |
Output is correct |
3 |
Correct |
15 ms |
1536 KB |
Output is correct |
4 |
Correct |
8 ms |
512 KB |
Output is correct |
5 |
Correct |
14 ms |
1280 KB |
Output is correct |
6 |
Correct |
15 ms |
1280 KB |
Output is correct |
7 |
Correct |
8 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
15 ms |
1536 KB |
Output is correct |
6 |
Correct |
15 ms |
1536 KB |
Output is correct |
7 |
Correct |
15 ms |
1536 KB |
Output is correct |
8 |
Correct |
8 ms |
512 KB |
Output is correct |
9 |
Correct |
14 ms |
1280 KB |
Output is correct |
10 |
Correct |
15 ms |
1280 KB |
Output is correct |
11 |
Correct |
8 ms |
512 KB |
Output is correct |
12 |
Correct |
220 ms |
18168 KB |
Output is correct |
13 |
Correct |
225 ms |
18168 KB |
Output is correct |
14 |
Correct |
213 ms |
18172 KB |
Output is correct |
15 |
Correct |
215 ms |
18296 KB |
Output is correct |
16 |
Correct |
53 ms |
2300 KB |
Output is correct |
17 |
Correct |
202 ms |
12920 KB |
Output is correct |
18 |
Correct |
194 ms |
13048 KB |
Output is correct |
19 |
Correct |
191 ms |
12924 KB |
Output is correct |
20 |
Correct |
198 ms |
12952 KB |
Output is correct |
21 |
Correct |
190 ms |
12920 KB |
Output is correct |
22 |
Correct |
58 ms |
2296 KB |
Output is correct |