File tree Expand file tree Collapse file tree 1 file changed +28
-6
lines changed Expand file tree Collapse file tree 1 file changed +28
-6
lines changed Original file line number Diff line number Diff line change 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
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ const int MX = 1'000'000 ;
8
+ int f[MX + 2 ], mx;
9
+
10
+ int main () {
8
11
ios::sync_with_stdio (0 );
9
12
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
+ */
You can’t perform that action at this time.
0 commit comments