-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1044.longest-duplicate-substring.TLE.cpp
More file actions
50 lines (46 loc) · 1.04 KB
/
1044.longest-duplicate-substring.TLE.cpp
File metadata and controls
50 lines (46 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode id=1044 lang=cpp
*
* [1044] Longest Duplicate Substring
*/
// @lc code=start
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
string longestDupSubstring(string s) {
int len = s.length();
int answerLen = 0;
int answerStart = 0;
int start = 0;
int low = 1;
int high = len;
while(low < high) {
if(answerLen + start >= len) {
break;
}
int mid = (low + high) >> 1;
for(int start = 0; start + mid < len; ++ start) {
auto it = search(s.begin() + start + 1, s.end(),
boyer_moore_horspool_searcher(
s.begin() + start, s.begin() + start + mid));
if(it != s.end()) {
answerStart = start;
answerLen = mid;
break;
}
}
if(answerLen == mid) {
low = mid + 1;
} else {
high = mid;
}
}
return s.substr(answerStart, answerLen);
}
};
// @lc code=end