Notice
Recent Posts
Recent Comments
09-29 03:12
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

Byeol Lo

N1305 - 광고 본문

Algorithm/KMP

N1305 - 광고

알 수 없는 사용자 2024. 5. 8. 00:03
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class N1305 {
    private static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    static String paragraph;
    static int n;

    public static void main(String[] args) throws IOException{
        n = Integer.parseInt(BR.readLine());
        paragraph = BR.readLine();
        System.out.print(n - getFailureScores(paragraph)[n-1]);
    }

    static int[] getFailureScores(String pattern) {
        int[] table = new int[pattern.length()];
        int j = 0;

        for (int i = 1; i < table.length; i++) {
            while (j > 0 && pattern.charAt(j) != pattern.charAt(i))
                j = table[j-1];

            if (pattern.charAt(i) == pattern.charAt(j))
                table[i] = ++j;
        }

        return table;
    }
}

'Algorithm > KMP' 카테고리의 다른 글

N1786 - 찾기  (0) 2024.05.11
N11585 - 속타는 저녁 메뉴  (0) 2024.05.08
Comments