[BOJ] 1701 Cubeditor

Time Lapse :14min 31sec

1701.cpp

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
50
51
52
53
54
#include <iostream>
#include <vector>
using namespace std;
vector<int> kmptable(string &str) {
int len = str.length(), j = 0;
vector<int> table(len, 0);
for(int i = 1; i < len; i++) {
while(j && str[i] != str[j])
j = table[j-1];
if(str[i] == str[j])
table[i] = ++j;
}
return table;
}
bool kmp(string &s, string p) {
int ret = 0;
auto table = kmptable(p);
int n = s.size(), m = p.size(), j = 0;
for(int i = 0; i < n; i++) {
while (j && s[i] != p[j])
j = table[j - 1];
if (s[i] == p[j])
if(j == m-1) {
ret++;
j = table[j];
if(ret >= 2) return true;
} else
j++;
}
return false;
}

int binarysearch(string& s) {
int l = 0, r = s.length(), m, ans = 0;
while(l < r) {
bool find = false;
m = (l + r) / 2;
for(int i = 0; i < s.length() - m && !find; i++)
find = kmp(s, s.substr(i,m));
if(find) {
l = m + 1;
ans = max(ans, m);
} else {
r = m;
}
}
return ans;
}
int main(int argc, char** argv){
string str;
cin>>str;
cout<<binarysearch(str);
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/01/PS/BOJ/1701/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.