Algorithm/Baekjoon
백준 1701번. Cubeditor(c++) / kmp
jungahshin
2020. 7. 27. 00:32
반응형
전형적인 kmp문제이다.
하지만 더 추가해야할 포인트가 있다.
입력받은 문자열에 대한 failure function을 작성해서 돌리되,
kmp Algorithm은 맨 처음 문자부터 시작하기 때문에 접미사를 하나씩 지우면서 최대값을 찾아나갔다.
일단, kmp Algorithm에 대한 개념이 생소하신 분은 아래 링크를 참고해서 공부하시면 될 것 같습니다.
https://jason9319.tistory.com/130
KMP(Knuth–Morris–Pratt) 알고리즘
KMP 알고리즘은 문자열 A와 문자열 B가 있을 때, 문자열 A에서 문자열 B를 찾아주는 알고리즘 입니다. KMP는 KMP를 만든 사람의 이름인 Knuth, Morris, Prett 세 사람의 앞 글자를 따서 KMP 알고리즘이라고 �
jason9319.tistory.com
코드 첨부
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
|
// Cubeditor
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string s, temp;
int final = 0;
vector<int> getPi(string p){//실패 함수 작성
int m = (int)p.size(), j=0;
vector<int> pi(m, 0);
for(int i = 1; i< m ; i++){
while(j > 0 && p[i] != p[j])
j = pi[j-1];
if(p[i] == p[j])
pi[i] = ++j;
}
return pi;
}
int main()
{
cin>>s;
temp = s;
for (int i=0; i < s.size(); i++) {
string temp = s.substr(i, s.size());
auto matched = getPi(temp);
for (int j=0; j <matched.size(); j++) {
final = max(final, matched[j]);
}
}
cout<<final<<"\n";
return 0;
}
|
cs |
github 첨부
https://github.com/jungahshin/algorithm/blob/master/c:c%2B%2B/1701.cpp
jungahshin/algorithm
algorithm study. Contribute to jungahshin/algorithm development by creating an account on GitHub.
github.com
반응형