-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1062.cpp
More file actions
46 lines (40 loc) · 951 Bytes
/
Copy path1062.cpp
File metadata and controls
46 lines (40 loc) · 951 Bytes
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
class Solution {
public:
//思路:确定一部分,再确定另一部分,两部分合起来就是搜索
bool handle(int l, string S)
{
set<string> ss;
for(int i = 0; i <= S.size()-1-l+1; ++i)
{
string s = S.substr(i, l);
if(ss.find(s) != ss.end())
{
return true;
}
ss.insert(s);
}
return false;
};
int longestRepeatingSubstring(string S) {
int s = 1, e = S.size()-1;
int l;
int max = 0;
while(e > s+1)
{
l = (s+e)/2;
bool ret = handle(l, S);
if(ret && l>max)max = l;
if(ret)
{
s = l;
}
else
{
e = l;
}
}
if(handle(s, S) && s > max)max = s;
if(handle(e, S) && e > max)max = e;
return max;
}
};