본문 바로가기
Computer Science

프양연 단어가 등장하는 횟수

by OKOK 2019. 1. 8.

1. 풀이 정리

2. 예외 사항 및 설계 완료

3. 풀이 -> 코딩 


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
#include <stdio.h>
#define MAX_N 10
#define MAX_S 10
 
//#define MAX_N 500010
//#define MAX_S 100010
 
char b[MAX_N], s[MAX_S];
int fail[MAX_S];
 
void make_fail_func() {
    fail[0= 0;
    int j = 0;
    for (int i = 1; s[i] != '\0'; i++) {
        while (j > 0 && s[i] != s[j]) j = fail[j - 1];
        if (s[i] == s[j]) fail[i] = ++j;
        else fail[i] = 0;
    }
}
 
int main() {
    freopen("input.txt""r", stdin);
    int T; scanf("%d"&T);
    for (int tc = 1; tc <= T; tc++) {
        scanf("%s", b);
        scanf("%s", s);
        make_fail_func();
        int j = 0;
        int cnt = 0;
        for (int i = 0; b[i] != '\0'; i++) {
            while (j > 0 && b[i] != s[j]) j = fail[j - 1];
            if (b[i] == s[j]) {
                j++;
                if (s[j] == '\0') {
                    j = fail[j - 1];
                    cnt++;
                }
            }
        }
 
        printf("#%d %d\n", tc, cnt);
    }
}
 
cs

 


'Computer Science' 카테고리의 다른 글

프양연 프리랜서  (0) 2019.01.11
프양연 두 번 이상 등장하는 문자열  (0) 2019.01.08
문자열 베이어 무어 / 계산 기하학  (0) 2019.01.03
프양연 Poker / 정리 후 if else  (0) 2018.12.24
프양연 Pole / DP  (0) 2018.12.24

댓글