8000 Merge pull request #424 from neppiness/1305 · sihyeon-kim/basic-algo-lecture@1011ad9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1011ad9

Browse files
Merge pull request encrypted-def#424 from neppiness/1305
update: 0x1E 1305.cpp
2 parents 1eb7689 + 7dc8f6b commit 1011ad9

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

0x1E/solutions/1305.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
2-
// Co-authored by : -
3-
// http://boj.kr/****************
1+
// Authored by : scsc3204
2+
// Co-authored by : BaaaaaaaaaaarkingDog
3+
// http://boj.kr/7e3f016c180b446fb38f8ae6b60291a6
44
#include <bits/stdc++.h>
55
using namespace std;
66

7-
int main(void){
7+
const int MX = 1'000'000;
8+
int f[MX + 2], mx;
9+
10+
int main() {
811
ios::sync_with_stdio(0);
912
cin.tie(0);
10-
11-
}
13+
14+
int n; string s;
15+
cin >> n >> s;
16+
17+
int j = 0;
18+
for(int i = 1; i < n; i++) {
19+
while(j > 0 && s[i] != s[j]) j = f[j - 1];
20+
if(s[i] == s[j]) f[i] = ++j;
21+
}
22+
cout << n - f[n - 1];
23+
}
24+
/*
25+
실패함수는 자기 자신을 제외한 접두사와 접미사가 일치하는 최대 길이입니다.
26+
광고판의 길이가 광고문구 보다 길면 앞부분부터 반복됩니다.
27+
따라서 실패함수를 통해 반복되기 시작하는 접두사의 길이를 확인할 수 있습니다.
28+
29+
실패함수의 마지막 인덱스 값은 문자열 전체 중에
30+
일치하는 접두사와 접미사의 최대 길이를 의미하므로,
31+
이 길이를 전체 문자열 길이에서 뺀 n - f[n - 1]의 값이
32+
곧 광고문구의 길이 중 가장 짧은 것의 길이가 됩니다.
33+
*/

0 commit comments

Comments
 (0)
0