#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int int64_t
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define F first
#define S second
#define I insert
#define PB push_back
#define POB pop_back
#define sqr(a) ((a) * (a))
#define P pop
#define max3(a, b, c) (max(a, max(b, c)))
#define max4(a, b, c, d) (max(max(a, b), max(c, d)))
#define min3(a, b, c) (min(a, min(b, c)))
#define min4(a, b, c, d) (min(min(a, b), min(c, d)))
#define MOD 1000000007
#define mod 998244353
int binpow(int a, int p, int m = MOD) {
int ans = 1;
while (p) {
if (p & 1) ans = ((ans % m) * (a % m)) % m;
a = sqr(a) % m;
p >>= 1;
}
return ans;
}
const int base = 31;
int inversebase;
vector<int> p(50, 1);
vector<map<int, int>> a, b;
vector<map<int, string>> rh;
int hs(string s) {
int h = 0;
for (int i = 0; i < s.size(); i++) {
h = (h + ((s[i] - 'a' + 1) * p[i]) % mod) % mod;
}
return h % mod;
}
pair<int, int> substr(int s, int n) {
pair<int, int> ans;
ans.F = ((s - (p[n] * (rh[n][s][n] - 'a' + 1)) % mod) % mod + mod) % mod;
ans.S =
(((s - (p[0] * (rh[n][s][0] - 'a' + 1) % mod) % mod) * inversebase) %
mod +
mod) %
mod;
return ans;
}
void solve() {
int n, k;
cin >> n >> k;
a.assign(n, map<int, int>());
b.assign(n, map<int, int>());
rh.assign(n, map<int, string>());
inversebase = binpow(base, mod - 2, mod);
for (int i = 1; i < 50; i++) p[i] = (p[i - 1] * base) % mod;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
string s;
cin >> s;
int h = hs(s);
b[i][h]++;
if (i == n - 1) a[i][h] = b[i][h];
rh[i][h] = s;
}
}
int ansi = 0;
for (int i = n - 1; i > 0; i--) {
for (auto x : a[i]) {
pair<int, int> cursub = substr(x.F, i);
a[i - 1][cursub.F] += b[i - 1][cursub.F] * x.S;
if (a[i - 1][cursub.F] == 0) a[i - 1].erase(cursub.F);
if (cursub.F != cursub.S)
a[i - 1][cursub.S] += b[i - 1][cursub.S] * x.S;
if (a[i - 1][cursub.S] == 0) a[i - 1].erase(cursub.S);
}
}
for (auto x : a[0]) ansi += x.S;
cout << ansi << endl;
}
int32_t main() {
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Compilation message
trener.cpp: In function 'int64_t hs(std::string)':
trener.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1372 KB |
Output is correct |
2 |
Correct |
7 ms |
1372 KB |
Output is correct |
3 |
Correct |
5 ms |
1368 KB |
Output is correct |
4 |
Incorrect |
4 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1372 KB |
Output is correct |
6 |
Correct |
7 ms |
1372 KB |
Output is correct |
7 |
Correct |
5 ms |
1368 KB |
Output is correct |
8 |
Incorrect |
4 ms |
344 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |