Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int n;
- const int MAX_LEN = 200010;
- const int P = 10007;
- char str[MAX_LEN];
- int exponent[MAX_LEN];
- void create_exponent(int n) {
- exponent[0] = 1;
- for (int i = 1; i < n; i++) {
- exponent[i] = (long long)(P) * exponent[i - 1];
- }
- }
- class Hash {
- vector<long long> v;
- public:
- Hash() {}
- void initialize(char* str) {
- this->v.resize(n, 0);
- this->v[0] = str[0];
- for (int i = 1; i < n; i++) {
- this->v[i] = this->v[i-1] * P + str[i];
- }
- }
- int get_hash(int loc, int len) {
- int h;
- if (loc == 0) {
- h = this->v[len - 1];
- } else {
- h = (this->v[loc + len - 1] - (this->v[loc - 1] * exponent[len]));
- }
- return h;
- }
- };
- Hash hc;
- bool find_length(int mid) {
- int hash_code[n - mid + 1];
- for (int i = 0; i < n - mid + 1; i++) {
- hash_code[i] = hc.get_hash(i, mid);
- }
- sort(hash_code, hash_code + n - mid + 1);
- for (int i = 0; i < n - mid; i++)
- if (hash_code[i] == hash_code[i + 1])
- return true;
- return false;
- }
- int main() {
- scanf("%d", &n);
- scanf("%s", &str);
- hc.initialize(str);
- create_exponent(MAX_LEN);
- int s = 0, e = n;
- int mid;
- while (s + 1 < e) {
- mid = (s + e) / 2;
- if (find_length(mid)) {
- s = mid;
- } else {
- e = mid;
- }
- }
- printf("%d\n", s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement