Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solved day 8 medium #176

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion medium/day_8/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
//Write your code here
//Write your code here
// Code by Jagpreet153
#include <bits/stdc++.h>
using namespace std;
const int P = 131;
typedef long long ll;
ll h[10010], p[10010];
int n;
string s;
pair<bool, int> check(int mid) {
unordered_map<ll, int> M;
for (int i = mid; i <= n; i++) {
ll hash = h[i] - h[i - mid] * p[mid];
if (M.count(hash)) return make_pair(true, M[hash]);
M[hash] = i - mid + 1;
}
return make_pair(false, -1);
}

int main() {
cin >> s;
s = " " + s;
n = s.size() - 1;
p[0] = 1;
for (int i = 1; i <= n; i++) {
h[i] = h[i - 1] * P + s[i];
p[i] = p[i - 1] * P;
}
int l = 0, r = n, start = -1;
while (l < r) {
int mid = (l + r + 1) >> 1;
pair<bool, int> result = check(mid);
if (result.first) l = mid, start = result.second;
else r = mid - 1;
}
for (int i = start; i < start + l; i++) cout << s[i];
return 0;
}
Loading