-
Notifications
You must be signed in to change notification settings - Fork 0
/
256.c
123 lines (109 loc) · 2.71 KB
/
256.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// http://www.bjfuacm.com/problem/256/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 1000
int main() {
char* a;
char* b;
int flag;
while (1) {
flag = 0;
a = (char *)calloc(LEN, sizeof(char));
b = (char *)calloc(LEN, sizeof(char));
scanf("%s %s", b, a);
if (a[0] == '0' && b[0] == '0') break;
int len_a = (int)strlen(a);
int len_b = (int)strlen(b);
for (int i = len_b; i < 2 * len_b; i++) {
b[i] = b[i - len_b];
}
for (int i = 0; i < len_a - len_b + 1; i++) {
for (int j = 0; j < len_b; j++) {
int ck = 1; // default match
for (int k = 0; k < len_b; k++) {
if (a[i + k] != b[j + k]) ck = 0;
}
if (ck) {
flag = 1;
break;
}
}
}
printf("%s\n", flag ? "YES" : "NO");
free(a);
free(b);
}
}
/*******
* KMP *
*******/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define LEN 1000
int* compute(int* lps, char* pat) {
int N = strlen(pat);
lps = (int *)calloc(N, sizeof(int));
lps[0] = 0;
int index = 0;
for (int i = 1; i < N; ) {
if (pat[index] == pat[i]) {
lps[i] = index + 1;
index += 1;
i += 1;
} else {
if (index != 0) {
index = lps[index - 1];
} else {
lps[i] = 0;
i += 1;
}
}
}
return lps;
}
bool KMP(char* txt, char* pat) {
int* lps = compute(lps, pat);
// lps refers to "the longest proper prefix which is also suffix"
int i = 0;
int j = 0;
while (i < strlen(txt) && j < strlen(pat)) {
if (txt[i] == pat[j]) {
i += 1;
j += 1;
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i += 1;
}
}
}
free(lps);
return j == strlen(pat);
}
int main() {
char* pat;
char* txt;
bool flag;
while (1) {
flag = 0;
pat = (char *)calloc(LEN, sizeof(char));
txt = (char *)calloc(LEN, sizeof(char));
scanf("%s %s", pat, txt);
if (pat[0] == '0' && txt[0] == '0') break;
int len_pat = strlen(pat);
int len_txt = strlen(txt);
for (int i = len_pat; i < 2 * len_pat; i++) {
// do loop pat
flag = KMP(txt, pat + i - len_pat) ? 1 : 0;
if (flag) break;
pat[i] = pat[i - len_pat];
}
free(pat);
free(txt);
printf("%s\n", flag ? "YES" : "NO");
}
}