# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
845489 |
2023-09-06T13:51:33 Z |
vjudge1 |
Trener (COCI20_trener) |
C++17 |
|
6 ms |
1372 KB |
#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;
const int base2 = 29;
int inversebase, inversebase2;
vector<int> p(100, 1), p2(100, 1);
vector<map<pair<int, int>, int>> a, b;
vector<map<pair<int, int>, string>> rh;
pair<int, int> hs(string s) {
int h = 0;
int h2 = 0;
for (int i = 0; i < s.size(); i++) {
h = (h + ((s[i] - 'a' + 1) * p[i]) % mod) % mod;
}
for (int i = 0; i < s.size(); i++) {
h2 = (h2 + ((s[i] - 'a' + 1) * p2[i]) % MOD) % MOD;
}
return {h, h2};
}
pair<pair<int, int>, pair<int, int>> substr(pair<int, int> s, int n) {
pair<pair<int, int>, pair<int, int>> ans;
ans.F.F =
((s.F - (p[n] * (rh[n][s][n] - 'a' + 1)) % mod) % mod + mod) % mod;
ans.S.F =
(((s.F - (p[0] * (rh[n][s][0] - 'a' + 1) % mod) % mod) * inversebase) %
mod +
mod) %
mod;
ans.F.S =
((s.S - (p2[n] * (rh[n][s][n] - 'a' + 1)) % MOD) % MOD + MOD) % MOD;
ans.S.S = (((s.S - (p2[0] * (rh[n][s][0] - 'a' + 1) % MOD) % MOD) *
inversebase2) %
MOD +
MOD) %
MOD;
return ans;
}
void solve() {
int n, k;
cin >> n >> k;
a.assign(n, map<pair<int, int>, int>());
b.assign(n, map<pair<int, int>, int>());
rh.assign(n, map<pair<int, int>, string>());
inversebase = binpow(base, mod - 2, mod);
inversebase2 = binpow(base2, MOD - 2, MOD);
for (int i = 1; i < 100; i++) p[i] = (p[i - 1] * base) % mod;
for (int i = 1; i < 100; i++) p2[i] = (p2[i - 1] * base2) % MOD;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
string s;
cin >> s;
pair<int, 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<pair<int, int>, 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 'std::pair<long int, long int> hs(std::string)':
trener.cpp:42: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]
42 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
trener.cpp:45: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]
45 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1372 KB |
Output is correct |
2 |
Correct |
6 ms |
1372 KB |
Output is correct |
3 |
Correct |
6 ms |
1356 KB |
Output is correct |
4 |
Incorrect |
4 ms |
444 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
1372 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
6 ms |
1356 KB |
Output is correct |
8 |
Incorrect |
4 ms |
444 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |