This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define PB push_back
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
const int N = 2000100;
const int md = 998244353;
const int PW = 233;
gp_hash_table<int, int> mem;
string s;
int n, suf[N], po[N], ans;
int mult(int x, int y) { return (1ll * x * y) % md; }
int sub(int x, int y){
x -= y;
if (x < 0)
x += md;
return x;
}
int sum(int x, int y){
x += y;
if (x >= md)
x -= md;
return x;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n;
po[0] = 1;
for (int i = 1; i < N; i++)
po[i] = mult(po[i - 1], PW);
for (int i = 0; i < n; i++){
cin >> s;
int len = sz(s);
suf[len] = 0;
for (int i = len - 1; i >= 0; i--)
suf[i] = sum((s[i] - 'A') + 1, mult(PW, suf[i + 1]));
int res = 1;
for (int l = 1; l <= len; l++){
int pf = sub(suf[0], mult(suf[l], po[l]));
int sf = suf[len - l];
if (pf != sf) continue;
if (mem.find(pf) == mem.end()) continue;
res = max(res, 1 + mem[pf]);
}
ans = max(ans, res);
mem[suf[0]] = max(mem[suf[0]], res);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |